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
 General SQL Server Forums
 New to SQL Server Programming
 Query

Author  Topic 

DSullSQL
Starting Member

3 Posts

Posted - 2012-02-17 : 09:52:21
Hi I am trying to run a query against data that is somewhat like this

Acct_NO Type
1 BIG
1 BIG
1 BIG
1 BIG
2 BIG
2 SMALL
3 SMALL
3 SMALL
3 SMALL
3 SMALL
3 BIG
4 BIG
4 BIG
4 BIG
4 BIG
5 SMALL
5 SMALL
5 SMALL
5 SMALL
6 BIG
6 BIG
6 BIG
6 BIG
6 SMALL
6 SMALL

As you can see some of the repeating accnt_no values have both BIG and SMALL types associated with them. What I want to do is write a query to count the number of unique account numbers that have both BIG and Small types associated with them.

Thanks,
D

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-17 : 10:05:53
[code]
SELECT Acct_NO, COUNT(DISTINCT Acct_NO)
FROM Table o
WHERE EXISTS (SELECT * FROM Table i1 WHERE o.Acct_NO = i1.Acct_NO AND Type = 'BIG')
AND EXISTS (SELECT * FROM Table i2 WHERE o.Acct_NO = i2.Acct_NO AND Type = 'SMALL')
GROUP BY Acct_NO
[/code]

???

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-17 : 13:15:25
[code]
SELECT COUNT(DISTINCT Accnt_No) AS Cnt
FROM
(
SELECT Acct_NO
FROM table
GROUP BY Acct_NO
HAVING COUNT(DISTINCT CASE WHEN Type IN ('BIG','SMALL') THEN 1 ELSE NULL END) = 2
)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -