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
 Case Statement - Multiple Conditions

Author  Topic 

rindharajan
Starting Member

6 Posts

Posted - 2010-03-17 : 09:49:33
I need to get the following from a table depending on the condition.

count(loan_num) as 60_cnt where num_days<60
count(loan_num) as 90_cnt where num_days>60

Can someone help me with the query? Is it possible to do with case?

Thanks,

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 09:57:56
count(case when num_days<60 then loan_num else null end) as 60_cnt ,
count(case when num_days>60 then loan_num else null end) as 90_cnt

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

Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-03-17 : 10:23:45
A different method with different result but maybe it'll help you out anyway... ->
SELECT 
Somecolumn,
60_cnt = SUM(CASE WHEN num_days < 60 THEN 1 ELSE 0 END),
90_cnt = SUM(CASE WHEN num_days >= 60 THEN 1 ELSE 0 END)
FROM mytable
WHERE loan_num IS NOT NULL
GROUP BY Somecolumn


- Lumbago
If the facts don't fit the theory, change the facts. Albert Einstein
Go to Top of Page
   

- Advertisement -