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)
 case count

Author  Topic 

iradev
Starting Member

45 Posts

Posted - 2010-11-17 : 09:03:18
Hi all,

I have two tables called Placements and Jobs. I want to find out the number of jobs placed within 6 months of registration and the number of jobs that werent placed within 6 months of registration. My current code looks like this:

SELECT CASE WHEN Placements.PDate <= DATEADD(month,6,Jobs.JDate) 
THEN 'Yes'
ELSE ''
END AS 'Not Filled'
,CASE WHEN Placements.PDate > DATEADD(month,6,Jobs.JDate)
THEN 'Yes'
ELSE ''
END AS 'Not Filled'


PDate is the Placement Date and Jdate is the job registration date. I need to convert the code so it counts all rows for 'Filled' and 'Not Filled' and outputs a single number instead of YES'es. Any help is appreciated.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-11-17 : 10:04:20
count() only count not null value. Just Return NULL for the ELSE or skip the entire else part.


select count( CASE WHEN Placements.PDate <= DATEADD(month,6,Jobs.JDate) THEN 'Yes' END)
from yourtable




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-17 : 10:16:19
select
sum( CASE WHEN Placements.PDate <= DATEADD(month,6,Jobs.JDate or Placements.PDate > DATEADD(month,6,Jobs.JDate) THEN 1 else 0 end END) yes_count,
sum( not(CASE WHEN Placements.PDate <= DATEADD(month,6,Jobs.JDate or Placements.PDate > DATEADD(month,6,Jobs.JDate)) THEN 1 else 0 end END) no_count
from yourtable


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -