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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using Count to total the same value in a column

Author  Topic 

phate06
Starting Member

17 Posts

Posted - 2013-07-24 : 05:06:11
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)

17689 Posts

Posted - 2013-07-24 : 05:13:39
[code]
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
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

phate06
Starting Member

17 Posts

Posted - 2013-07-24 : 05:31:21
Brilliant thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-24 : 05:43:42
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
   

- Advertisement -