Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Erland Sommarskog Newsgroups: comp.databases.ms-sqlserver Subject: Re: SELECT DISTINCT and compound primary key. Date: Tue, 07 Jun 2011 23:37:33 +0200 Organization: Erland Sommarskog Lines: 41 Message-ID: References: <5cafddb1-1d17-4a9c-bed5-fbb18cf49311@34g2000pru.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Injection-Info: mx04.eternal-september.org; posting-host="DD6dU+BfJNjsjSP4/K/V7w"; logging-data="3616"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19TdRwHw3Wqpzugz4tMbDC5" User-Agent: Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32) Cancel-Lock: sha1:C4+e+WgeujSOhyykbjyVFBJqQ4E= Xref: x330-a1.tempe.blueboxinc.net comp.databases.ms-sqlserver:408 Richard (richardemig@gmail.com) writes: > I have a table (Main) with compound key and second, temporary table > (Temp) with same fields but without PK. Main table key is 3 columns > ( SourceFileHash, FileHash, PathHash ). There is also 4 other columns > in each table. > My intent is to use temp table to store batches of records with > duplicates and inserting only unique records to main table after every > batch is finished ( batches are inserted using SqlBulkCopy ). > > How can I select distinct records on Temp table but using only 3 PK > columns ? > > Here is query that I created - it won't work because it is trying to > insert duplicate records to main: WITH numbered AS ( SELECT sourceFileHash, pathHash, fileHash, length, creationDate, modificationDate, lastAccessDate, line, row_number() OVER (PARTITION BY sourceFileHash, pathHash, fileHash ORDER BY ?????) as rowno FROM CatalogEntriesTemp ) SELECT sourceFileHash, pathHash, fileHash, length, creationDate, modificationDate, lastAccessDate, line FROM numbered WHERE rowno = 1 You need to fill in the ORDER BY clause to determine which of the rows with the same duplicate key you want. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Links for SQL Server Books Online: SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx