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
 Get 2 different COUNT's for the same column

Author  Topic 

FernandoLorival
Starting Member

19 Posts

Posted - 2011-02-19 : 19:38:04
Hi All,
I have a movies table that contains MovieID, MovieName, Status.
Status tells me if the movie is IN or OUT.
I want to build a query that gives me 3 columns: MovieID, IN, OUT

My table looks like this:

MovieID -- MovieName -- Status
1 -- Bla -- IN
2 -- Bli -- IN
1 -- Bla -- OUT
1 -- Bla -- OUT
......

Something like this:

MovieID -- IN -- OUT
1 -- 6 -- 4
2 -- 7 -- 9

It looks simple but i cant get around the fact that i need 2 WHERE cluases in the same query!!
Thank you all!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-02-19 : 21:48:58
SELECT MovieID, SUM(CASE WHEN Status='IN' THEN 1 END) IN,
SUM(CASE WHEN Status='OUT' THEN 1 END) OUT
FROM Movies
GROUP BY MovieID
Go to Top of Page

RobertKaucher
Posting Yak Master

169 Posts

Posted - 2011-02-19 : 22:10:33
robvolk's query was much better - simpler! I retract mine. ;-)
Go to Top of Page

FernandoLorival
Starting Member

19 Posts

Posted - 2011-02-19 : 22:17:20
That works!
Thank you soo much!!!
Go to Top of Page
   

- Advertisement -