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.
| Author |
Topic |
|
ljp099
Yak Posting Veteran
79 Posts |
Posted - 2011-11-09 : 12:42:52
|
| I have a table, Contracts, with the following data:SfId | InternalIdabc123 1400abc123 1400abc123 1500abc123 1500The SfId should be unique for a given InternalId. I use this query to give me the distinct InternalIds.Select distinct InternalId from Contractsreturns:InternalId14001500Now what I need is a query to return the InternalId of records where the SfId is not unique grouped by InternalId (if the same SfId value exists in more than 1 distinct InternalId, then it should appear as a result in this query).This table data:SfId | InternalIdabc123 1400abc123 1400abc123 1500abc123 1500should return:14001500 since both have the SfId value: abc123 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 12:45:48
|
| [code]SELECT DISTINCT InternalIdFROM table tINNER JOIN (SELECT SfId FROM table GROUP BY SfId HAVING COUNT(DISTINCT InternalId)> 1)t1ON t1.SfId = t.SfId[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ljp099
Yak Posting Veteran
79 Posts |
Posted - 2011-11-09 : 13:29:37
|
| Thanks for your help! That is exactly the query I was trying to write but couldn't. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 13:32:10
|
no problemyou're welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|