<p>OK, here is my problem, without being too specific for reasons of being fired for putting company practices on the internet.</p>

<p>There are spreadsheets made. These are uploaded to the database. I need to filter out duplicates from uploading. The only way to do this is by making sure that for each and every entry that two fields aren't the same as an entry already in the database. As just becasue one field is the same does not mean its a duplicate. There are two specific fields lets call them FLDA and FLDB that both must match up to an entry in the database already. I can filter by one field already. I'm thinking this has to be a subquery but I'm not sure how to apply it.
This is hard to decribe. Just ask if your not sure what I mean.</p>

[{'answer_id': 22775, 'author': 'BIBD', 'author_id': 685, 'author_profile': 'https://Stackoverflow.com/users/685', 'pm_score': 1, 'selected': False, 'text': "<p>I had a similar problem. My solution was to:</p>\n\n<ol>\n<li>import into a staging-table. </li>\n<li>delete the duplicates</li>\n<li>copy what's left over into the live table</li>\n</ol>\n\n<p>It's a little BFI, but it just plain works.</p>\n"}, {'answer_id': 22785, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 0, 'selected': False, 'text': '<p>How are you loading them into the database? Is this with your own code to read the Excel files? You can <a href="https://stackoverflow.com/questions/15828/reading-excel-files-from-c#15839">read the Excel files using ADO/ADO.NET with the right connection string</a>. Then you could read the data using a query that would weed out the dupes. </p>\n'}, {'answer_id': 24618, 'author': 'Jason Z', 'author_id': 2470, 'author_profile': 'https://Stackoverflow.com/users/2470', 'pm_score': 0, 'selected': False, 'text': "<p>Building on CodeSlave's answer, Access provides a Find Duplicates Query wizard that can help you easily build the query to weed out the duplicates.</p>\n\n<p>Another approach would be to set up an identity on FLDA and FLDB.  This will prevent duplicate entries from even getting written to the table.  Of course you will also need to catch the exception that is thrown when the insert operation fails.</p>\n"}, {'answer_id': 24673, 'author': 'Fionnuala', 'author_id': 2548, 'author_profile': 'https://Stackoverflow.com/users/2548', 'pm_score': 1, 'selected': False, 'text': "<p>Would a query suit? For example:</p>\n\n<pre><code>INSERT INTO ToUpdate ( Field1, Field2 )\nSELECT e.H1, e.H2\nFROM (SELECT * FROM [Sheet1$] IN '' [Excel 8.0;HDR=YES;IMEX=1;database=C:\\Docs\\LTD.xls]) As e\nLEFT JOIN ToUpdate ON (e.H2 = ToUpdate.Field2) AND (e.H1 = ToUpdate.Field1)\nWHERE ToUpdate.Field1 Is Null AND ToUpdate.Field2 Is Null\n</code></pre>\n"}, {'answer_id': 26101, 'author': 'kaybenleroll', 'author_id': 277, 'author_profile': 'https://Stackoverflow.com/users/277', 'pm_score': 0, 'selected': False, 'text': "<p>Is there a field FLDC that would be different for identifying duplicates? I presume there must be, as otherwise having (FLDA,FLDB) as a unique or primary key would solve your problem immediately.</p>\n\n<p>Assuming there is such a field, you could try something like this:</p>\n\n<pre><code>SELECT T1.FLDA, T1.FLDB, T1.FLDC\nFROM Table1 T1, Table1 T2\nWHERE T1.FLDA = T2.FLDA\n  AND T1.FLDB = T2.FLDB\n  AND T1.FLDC &lt;&gt; T2.FLDC\n</code></pre>\n\n<p>The downside here is that both the original and the duplicate will be returned by something like this. If you only want to see the duplicates, you will probably have to figure out a way to identify an 'original' row and add another WHERE clause or two for this.</p>\n\n<p>If you can get a query that gives you just the duplicate rows and not the originals, it should be pretty easy to change it to a DELETE query.</p>\n"}, {'answer_id': 70187, 'author': 'Chris OC', 'author_id': 11041, 'author_profile': 'https://Stackoverflow.com/users/11041', 'pm_score': 0, 'selected': False, 'text': '<p>To avoid duplicates on imports:</p>\n\n<p>1 - If there\'s not already a primary key on the table, put one on FLDA and FLDB (together).  If there\'s already a primary key that\'s not FLDA and FLDB (together), place an index on the table on these two fields, unique yes, ignore nulls no.</p>\n\n<p>2 - You can import from the spreadsheet to the table with the wizard or with a query.  If you do it with the import spreadsheet wizard, you\'ll see this message before the import starts:</p>\n\n<p>"DB name was unable to append all the data to the table.</p>\n\n<p>"The contents of fields in 0 records were deleted and (xx) records were lost due to key violations.  (These lost records were duplicates, so no real loss there.)  ... Do you want to proceed anyway?"</p>\n\n<p>Click yes to import the rows from the spreadsheet.  No duplicates will be imported.</p>\n\n<p>Or, to use a query for the import, paste this into a new query in sql view (menu:  Insert > Query > Design View, Close button; menu:  View > SQL View.)</p>\n\n<pre><code>INSERT INTO tblInput\n  SELECT XLS.* \n  FROM tblInput AS T RIGHT JOIN\n    [Excel 8.0;IMEX=1;HDR=Yes;DATABASE=c:\\data.xls;].[Sheet1$] AS XLS \n    ON T.FLDA = XLS.FLDA AND T.FLDB = XLS.FLDB\n  WHERE ISNULL(T.FLDA) AND ISNULL(T.FLDB);\n</code></pre>\n\n<p>Change the path, c:\\data.xls, to your path, Sheet1$ to your sheetname, tblInput to your table name, and FLDA and FLDB to your column names.  If the spreadsheet doesn\'t have headers (column names) change HDR=Yes to HDR=No</p>\n'}, {'answer_id': 149430, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I did this by using a delete query and then using <code>Select from Table 1 Group by X Having Y, Z, A</code>. And then I put run query button for front end users.</p>\n\n<p>Cheers for all your help.</p>\n'}]
['https://Stackoverflow.com/questions/22696', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']