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 |
|
rickystyx
Starting Member
4 Posts |
Posted - 2012-04-12 : 17:30:21
|
| Hopefully a simple COUNT function is the answer here...TABLE.ColumnA contains unique employee identifiersTABLE.ColumnB contains value either 10 or 20I want to select three columns:Identifier, Count of 10, Count of 20FROM TABLEWHERE (criteria)GROUP BY Identifier--Any help would be appreciated! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-12 : 17:35:56
|
| [code]SELECT ColumnA,COUNT(CASE WHEN ColumnB=10 THEN 1 END) AS [Cnt10],COUNT(CASE WHEN ColumnB=20 THEN 1 END) AS [Cnt20]FROM TableGROUP BY ColumnA[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rickystyx
Starting Member
4 Posts |
Posted - 2012-04-12 : 17:50:18
|
| Perfect. Thanks so much! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-13 : 11:36:51
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-04-17 : 04:28:08
|
quote: Originally posted by visakh16
SELECT ColumnA,COUNT(CASE WHEN ColumnB=10 THEN 1 END) AS [Cnt10],COUNT(CASE WHEN ColumnB=20 THEN 1 END) AS [Cnt20]FROM TableGROUP BY ColumnA ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
The above may return warnings on NULLs. I prefer thisSELECT ColumnA,SUM(CASE WHEN ColumnB=10 THEN 1 ELSE 0 END) AS [Cnt10],SUM(CASE WHEN ColumnB=20 THEN 1 ELSE 0 END) AS [Cnt20]FROM TableGROUP BY ColumnAMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|