| Author |
Topic  |
|
|
Steve2106
Posting Yak Master
United Kingdom
149 Posts |
Posted - 11/16/2012 : 17:40:45
|
Hi There, I need to count more than 1 thing. This code gives me the first part of what I need: SELECT dbo.tbLocSource.SourceDescription, COUNT(*) AS Expr1 FROM dbo.tbLocal INNER JOIN dbo.tbLocSource ON dbo.tbLocal.SourceId = dbo.tbLocSource.SourceId GROUP BY dbo.tbLocSource.SourceDescription ORDER BY Expr1 DESC
But now I need to count the number of High, Medium and Low there are on the count returned.
Thanks for your help.
Best Regards,
Steve |
|
|
jimf
Flowing Fount of Yak Knowledge
USA
2868 Posts |
Posted - 11/16/2012 : 17:55:15
|
If High, Medium and Low are values in SourceDescription you can do this SELECT SUM(CASE WHEN SourceDesription = 'High' THEN 1 ELSE 0 END) AS HIGH , SUM(CASE WHEN SourceDesription = 'Medium' THEN 1 ELSE 0 END) AS Medium , SUM(CASE WHEN SourceDesription = 'Low' THEN 1 ELSE 0 END) AS Low ,COUNT(*) FROM dbo.tbLocal INNER JOIN dbo.tbLocSource ON dbo.tbLocal.SourceId = dbo.tbLocSource.SourceId
GROUP BY SourceDescription
Jim
Everyday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
48064 Posts |
Posted - 11/16/2012 : 22:38:39
|
quote: Originally posted by jimf
If High, Medium and Low are values in SourceDescription you can do this SELECT SUM(CASE WHEN SourceDesription = 'High' THEN 1 ELSE 0 END) AS HIGH , SUM(CASE WHEN SourceDesription = 'Medium' THEN 1 ELSE 0 END) AS Medium , SUM(CASE WHEN SourceDesription = 'Low' THEN 1 ELSE 0 END) AS Low ,COUNT(*) FROM dbo.tbLocal INNER JOIN dbo.tbLocSource ON dbo.tbLocal.SourceId = dbo.tbLocSource.SourceId
GROUP BY SourceDescription
Jim
Everyday I learn something that somebody else already knew
this will simply give a 1 against high,medium,low depending on what description value is due to group by on description field
my guess is high,low,etc are in some other field otherwise op will just get single row with all counts by removing the group by
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
Steve2106
Posting Yak Master
United Kingdom
149 Posts |
Posted - 11/17/2012 : 05:36:41
|
Hi Visakh,
As always, you are correct. The high, medium and low is stored in a field called priority on each record.
Any idea how I can do this.
Best Regards,
Steve |
Edited by - Steve2106 on 11/17/2012 05:37:22 |
 |
|
|
madhivanan
Premature Yak Congratulator
India
22469 Posts |
Posted - 11/17/2012 : 06:05:07
|
Then replace that column with Jim's code
SELECT SUM(CASE WHEN priority = 'High' THEN 1 ELSE 0 END) AS HIGH , SUM(CASE WHEN priority = 'Medium' THEN 1 ELSE 0 END) AS Medium , SUM(CASE WHEN priority = 'Low' THEN 1 ELSE 0 END) AS Low ,COUNT(*) FROM dbo.tbLocal INNER JOIN dbo.tbLocSource ON dbo.tbLocal.SourceId = dbo.tbLocSource.SourceId
GROUP BY SourceDescription
Madhivanan
Failing to plan is Planning to fail |
 |
|
| |
Topic  |
|