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)
 Group and Count Column Values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

randolphoralph
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

sodeep
Flowing Fount of Yak Knowledge

USA
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  
 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