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)
 Using Count to total the same value in a column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

phate06
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

khtan
In (Som, Ni, Yak)

Singapore
17638 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



KH
Time is always against us

Go to Top of Page

phate06
Starting Member

17 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/24/2013 :  05:43:42  Show Profile  Reply with Quote
you can use pivot too
[code
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
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.05 seconds. Powered By: Snitz Forums 2000