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
 Transact-SQL (2012)
 multiple counts
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

esthera
Flowing Fount of Yak Knowledge

1395 Posts

Posted - 05/17/2013 :  03:28:04  Show Profile  Reply with Quote
i have a table

name
response
response2
response3

now i want to make query where i retur

name response response2 response3

and for each name i put the count of how many records had a response for each of them


can i do a group by with a count of different fields where they are not null?

bandi
Flowing Fount of Yak Knowledge

India
2224 Posts

Posted - 05/17/2013 :  03:33:26  Show Profile  Reply with Quote
You can use COUNT(Specific ColumnName) OVER(PARTITION BY name)

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/17/2013 :  03:38:50  Show Profile  Reply with Quote

SELECT name,
SUM(CASE WHEN response > '' THEN 1 ELSE 0 END) AS response,
SUM(CASE WHEN response2 > '' THEN 1 ELSE 0 END) AS response2,
SUM(CASE WHEN response3 > '' THEN 1 ELSE 0 END) AS response3
FROM Table
GROUP BY name




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

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/17/2013 :  03:39:39  Show Profile  Reply with Quote
if you want to make it dynamic

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

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

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1782 Posts

Posted - 05/17/2013 :  18:32:48  Show Profile  Reply with Quote
If your "empty" are indeed NULL then you can use:
select count(*) cAll, count(response) cnt1, count(response2) cnt2, count(response3) cnt3
from MyTable
The aggregate functions ignore null columns (except count(*) of course).

=================================================
I am not one of those who in expressing opinions confine themselves to facts. (Mark Twain)
Go to Top of Page

esthera
Flowing Fount of Yak Knowledge

1395 Posts

Posted - 05/19/2013 :  05:34:36  Show Profile  Reply with Quote
thanks - these were very helpful responses
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/20/2013 :  01:13:35  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
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.06 seconds. Powered By: Snitz Forums 2000