Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.databases.ms-sqlserver > #408
| From | Erland Sommarskog <esquel@sommarskog.se> |
|---|---|
| Newsgroups | comp.databases.ms-sqlserver |
| Subject | Re: SELECT DISTINCT and compound primary key. |
| Date | 2011-06-07 23:37 +0200 |
| Organization | Erland Sommarskog |
| Message-ID | <Xns9EFDF055B2D56Yazorman@127.0.0.1> (permalink) |
| References | <5cafddb1-1d17-4a9c-bed5-fbb18cf49311@34g2000pru.googlegroups.com> |
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
Back to comp.databases.ms-sqlserver | Previous | Next — Previous in thread | Next in thread | Find similar
SELECT DISTINCT and compound primary key. Richard <richardemig@gmail.com> - 2011-06-07 05:07 -0700
Re: SELECT DISTINCT and compound primary key. "Bob Barrows" <reb01501@NOyahooSPAM.com> - 2011-06-07 08:54 -0400
Re: SELECT DISTINCT and compound primary key. "Fred." <ghrno-google@yahoo.com> - 2011-06-07 06:13 -0700
Re: SELECT DISTINCT and compound primary key. Erland Sommarskog <esquel@sommarskog.se> - 2011-06-07 23:37 +0200
Re: SELECT DISTINCT and compound primary key. Richard <richardemig@gmail.com> - 2011-06-10 07:50 -0700
csiph-web