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)
 troublesome query

Author  Topic 

joe@jjfsolutions.com
Starting Member

1 Post

Posted - 2009-02-02 : 11:41:04
I have a table that has the following columns.
gId(int PK), dealId(int can be dupes), country( varchar can be dupes)

The query needs to return all DealId where country contains only US and/or Japan
So if dealId 123 had 3 records with country of US, Japan, Germany dealId 123 should not be returned at all.

If dealid 456 had 2 records with country US and Japan it would be returned.

Any help is greatly appreciated I can't seem to get this one.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-02 : 11:55:28
[code]
SELECT dealId
FROM table
GROUP BY dealId
HAVING COUNT(DISTINCT CASE WHEN country IN ('US','Japan') THEN country ELSE NULL END) =2
AND SUM(CASE WHEN country IN ('US','Japan') THEN 0 ELSE 1 END)=0
[/code]
Go to Top of Page
   

- Advertisement -