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 2012 Forums
 SQL Server Administration (2012)
 multiple counts with join

Author  Topic 

Blessed1978
Yak Posting Veteran

97 Posts

Posted - 2014-06-16 : 23:34:01
i have the following SELECTs i would like to place them in just on statement

select count(*) as 'TOTAL EMPLOYEES' from employee

SELECT COUNT(*) AS 'TOTAL PEOPLE CALLED' FROM employee_DETAILS
where call_type = 'call'

SELECT COUNT(*) AS 'TOTAL PEOPLE met' FROM employee_DETAILS
where call_type = 'meeting'

so i created this query

select count(E.employees) as 'TOTAL EMPLOYEES',
COUNT(CASE WHEN ed.type = call then 1 else 0) end as 'TOTAL PEOPLE CALLED',
COUNT(CASE WHEN ed.type = meeting then 1 else 0)end as 'TOTAL PEOPLE met'
from employees e
inner join employee_details ed on e.employee_id = ed.employee_id

however when i run this all counts are 0 please advise


stepson
Aged Yak Warrior

545 Posts

Posted - 2014-06-17 : 01:11:18
[code]
COUNT(CASE WHEN call_typeed.type = 'call' then 1 else 0) end as 'TOTAL PEOPLE CALLED',
COUNT(CASE WHEN call_typeed.type = 'meeting' then 1 else 0)end as 'TOTAL PEOPLE met'
[/code]


At my first view, you forgot the '
and in first query the predicate is call_type ed.type

It's a typo ?






sabinWeb MCP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-06-21 : 05:30:18
also as you're using COUNT function it should be 1 and NULL rather than 1 and 0 as in latter case it will count 0s too and you'll get same count value for all

select count(E.employees) as 'TOTAL EMPLOYEES',
COUNT(CASE WHEN ed.call_type = 'call' then 1 else NULL end) as 'TOTAL PEOPLE CALLED',
COUNT(CASE WHEN ed.call_type = 'meeting' then 1 else NULL end) as 'TOTAL PEOPLE met'
from employees e
inner join employee_details ed on e.employee_id = ed.employee_id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -