Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Group and Count Column Values
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

7 Posts

Posted - 11/20/2012 :  18:58:59  Show Profile  Reply with Quote
I am not sure how to make my query group and count if certain values are found in the OrderID column.

Here is what I have so far.

Select FirstName, LastName, Count(OrderID) as PT, Count(OrderID) as TO, Count(OrderID) as TS

From Tracking

Where OrderID in ('PT', 'TO', 'TS')

Group by FirstName, LastName

Here is an example of the table

FirstName LastName OrderID
Billy Smith PT
Billy Smith PT
Billy Smith TO
Sally Smith PT
Sally Smith TS
Sally Smith TS

Here is what I would like from the output of my query

FirstName LastName PT TO TS
Billy Smith 2 1 0
Sally Smith 1 0 2

Edited by - randolphoralph on 11/20/2012 19:01:51

Flowing Fount of Yak Knowledge

7174 Posts

Posted - 11/20/2012 :  19:31:51  Show Profile  Reply with Quote

Select FirstName,LastName
,SUM(Case When OrderId = 'PT' then 1 else 0 End)as PT
,SUM(Case When OrderId = 'TO' then 1 else 0 End)as TO
,SUM(Case When OrderId = 'TS' then 1 else 0 End)as TS
from Tracking
Group by FirstName,LastName
Order by FirstName,LastName

Edited by - sodeep on 11/20/2012 19:32:17
Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000