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.
Author |
Topic |
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2013-05-17 : 03:28:04
|
i have a tablenameresponseresponse2response3now i want to make query where i returname response response2 response3and for each name i put the count of how many records had a response for each of themcan i do a group by with a count of different fields where they are not null? |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-05-17 : 03:33:26
|
You can use COUNT(Specific ColumnName) OVER(PARTITION BY name)--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-17 : 03:38:50
|
[code]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 response3FROM TableGROUP BY name[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2013-05-17 : 18:32:48
|
If your "empty" are indeed NULL then you can use:[CODE]select count(*) cAll, count(response) cnt1, count(response2) cnt2, count(response3) cnt3from MyTable[/CODE]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) |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2013-05-19 : 05:34:36
|
thanks - these were very helpful responses |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-20 : 01:13:35
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|