Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #402

Re: SELECT DISTINCT and compound primary key.

From "Bob Barrows" <reb01501@NOyahooSPAM.com>
Newsgroups comp.databases.ms-sqlserver
Subject Re: SELECT DISTINCT and compound primary key.
Date 2011-06-07 08:54 -0400
Organization A noiseless patient Spider
Message-ID <isl725$mei$1@dont-email.me> (permalink)
References <5cafddb1-1d17-4a9c-bed5-fbb18cf49311@34g2000pru.googlegroups.com>

Show all headers | View raw


Richard wrote:
> Hello,
>
> 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:
>
> SELECT temp.sourceFileHash, temp.pathHash, temp.fileHash, temp.
> [length], temp.creationDate, temp.modificationDate,
> temp.lastAccessDate, temp.line
> FROM CatalogEntriesTemp temp
> LEFT OUTER JOIN CatalogEntries main ON main.pathHash = temp.pathHash
> and main.fileHash = temp.fileHash AND main.sourceFileHash =
> temp.sourceFileHash
> WHERE main.pathHash IS NULL AND main.fileHash IS NULL AND
> main.sourceFileHash is null
>
> Thanks for any help in advance.

You need to create a CTE or view that uses GROUP BY to select the unique
rows from CatalogEntriesTemp, aggregating the non-key columns so that a
single row is retrieved.

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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