Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

esthera
Flowing Fount of Yak Knowledge

1410 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
2242 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
52326 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
52326 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
1834 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

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