SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Count more than 1
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Steve2106
Posting Yak Master

United Kingdom
170 Posts

Posted - 11/16/2012 :  17:40:45  Show Profile  Reply with Quote
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
2869 Posts

Posted - 11/16/2012 :  17:55:15  Show Profile  Reply with Quote
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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/16/2012 :  22:38:39  Show Profile  Reply with Quote
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/

Go to Top of Page

Steve2106
Posting Yak Master

United Kingdom
170 Posts

Posted - 11/17/2012 :  05:36:41  Show Profile  Reply with Quote
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
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22769 Posts

Posted - 11/17/2012 :  06:05:07  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000