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 2005 Forums
 Transact-SQL (2005)
 COUNT/Group by / several parameters....(SOLVED)

Author  Topic 

carvals
Starting Member

2 Posts

Posted - 2007-07-24 : 18:56:46
Hi,
I'm developping an application and I've got some trouble with the database. I would like to retrieve data to build a graph.

A sample of data

Type1_______Type2
P1__________ A
P2__________ B
P3__________ A
P1__________ A



I would like a result like:

Type1________ A ________ B
P1 __________ 2 ________ 0
P2 __________ 0 ________ 1
P3 __________ 1 ________ 0

with this function I can create the first and the second column:

SELECT Type1, COUNT(Type2) AS A
FROM Table1
WHERE (Type2= 'A')
GROUP BY Type1

But I've got some trouble to create the third one... I tried subquery but doen't work with the "group by"... Please HELP!!!!
Thanks..

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-24 : 19:03:10
[code]SELECT Type1,
[A] = SUM(CASE WHEN Type2 = 'A' THEN 1 ELSE 0 END),
[B] = SUM(CASE WHEN Type2 = 'B' THEN 1 ELSE 0 END)
FROM Table1
GROUP BY Type1
[/code]


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

Go to Top of Page

carvals
Starting Member

2 Posts

Posted - 2007-07-24 : 19:16:58
Hi KhTan

Great!!! thanks!

To easy for you I guess...

Go to Top of Page
   

- Advertisement -