Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Question about duplicates in UNION

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2010-09-23 : 08:52:48
Hi,

I have this query

SELECT DISTINCT [type] COLLATE Latin1_General_CI_AS as [type],capid FROM tblPricing
UNION
SELECT DISTINCT [type] COLLATE Latin1_General_CI_AS as [type],capid FROM dbPubMatrix..tblNewMatrixSpecial

If both tables contain 'type1',99999 for example will that record only appear once in this query?

Thanks

Kristen
Test

22859 Posts

Posted - 2010-09-23 : 12:02:45
Yes. UNION will sort remove duplicates. If you want to keep duplicates (or you know there will not be any) use UNION ALL - which is faster as it doesn't need to Sort/De-dupe the results)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-09-23 : 12:03:57
Probably no point you having DISTINCT on each SELECT, given that the UNION will remove the DUPs too - so potentially you have 3 Sorts & De-dups, whereas one would do - but that's assuming that SQL won't optimise it to only do one Sort & De-dupe
Go to Top of Page
   

- Advertisement -