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 2000 Forums
 Transact-SQL (2000)
 CONDITIONAL COUNT

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-06-14 : 07:37:06
glorijaa writes "Hi everyone,

I have a table tracking transactions made by customers, having fields like customerid,transaction_type, date, amount....
There are 2 types of transactions 'X' or 'Y'.
How can I make a select which would count total number of transactions made by each customer (count (*) group by customerid)), however I need two count result columns one for <where type='X'> and other for <where type='Y'>
Example:

Customerid Number of X Number of Y
000001 5 3
015971 58 71


Thanks"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-06-14 : 07:59:46
[code]
Select CustomerId,Sum(case when type='X' then 1 else 0 end) as 'Number of X',
Sum(case when type='Y' then 1 else 0 end) as 'Number of Y' from yourTable
Group by CustomerId[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -