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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using Count to total the same value in a column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

17 Posts

Posted - 07/24/2013 :  05:06:11  Show Profile  Reply with Quote
Hi there,
on one column named orderstatus, depending on the status of the order it will be either 14,15 or 16.

I need to be able to add up how many of 14, how many of 15 and how many of 16 there are in separate columns on my query.

I hope that makes sense

In (Som, Ni, Yak)

17684 Posts

Posted - 07/24/2013 :  05:13:39  Show Profile  Reply with Quote

select  count(case when orderstatus = 14 then 1 end) as [status 14],
        count(case when orderstatus = 15 then 1 end) as [status 15],
        count(case when orderstatus = 16 then 1 end) as [status 16]
from    yourtable

Time is always against us

Go to Top of Page

Starting Member

17 Posts

Posted - 07/24/2013 :  05:31:21  Show Profile  Reply with Quote
Brilliant thank you
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 07/24/2013 :  05:43:42  Show Profile  Reply with Quote
you can use pivot too
select [14] as [status 14],
[15] as [status 15],
[16] as [status 16]
from yourtable t
pivot(count(pkcol) for orderstatus IN (14,15,16))p

SQL Server MVP
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.03 seconds. Powered By: Snitz Forums 2000