SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 SQL Server Administration (2012)
 multiple counts with join
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Blessed1978
Yak Posting Veteran

52 Posts

Posted - 06/16/2014 :  23:34:01  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

Romania
403 Posts

Posted - 06/17/2014 :  01:11:18  Show Profile  Reply with Quote

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'



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

India
52309 Posts

Posted - 06/21/2014 :  05:30:18  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000