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 |
sqlslick
Yak Posting Veteran
83 Posts |
Posted - 2014-03-10 : 14:03:06
|
Hello,Consider the following dataset:COL1 | COL2 | COL3 | COL41 | FD | DR. A | Y2 | FD | DR. A | Y3 | FD | DR. A | N4 | FD | DR. A | Y5 | FD | DR. A | Y6 | PF | DR. A | Y7 | FD | DR. B | Y8 | PF | DR. B | NConsider the script below:SELECTCOL2, COL3, COUNT(COL1) AS TOTALSFROM CASESGROUP BY COL2, COL3ORDER BY COL3, COL2The script above produces the following output:COL2 | COL3 | TOTALSFD | DR. A | 5PF | DR. A | 1FD | DR. B | 1PF | DR. B | 1I need to add one more column to the script that counts records with 'Y' in COL4 for each COL1 category (FD, PF). The final dataset would look like this:COL2 | COL3 | TOTALS | NEWCOLFD | DR. A | 5 | 4PF | DR. A | 1 | 1FD | DR. B | 1 | 1PF | DR. B | 1 | 0I am having a hard time trying to use COUNT() on multiple columns with the GROUP BY restrictions that exist. Your help will be immensely aprpeciated.Thank you!!! |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2014-03-10 : 14:19:02
|
Does this work? SELECTCOL2, COL3, COUNT(COL1) AS TOTALS,SUM(CASE when Col4='Y' then 1 else 0 end) as NewColCheersMIK |
 |
|
sqlslick
Yak Posting Veteran
83 Posts |
Posted - 2014-03-10 : 15:06:46
|
Yes, that is perfect, thank you so much!!! |
 |
|
|
|
|