Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

column value count 1

Status
Not open for further replies.

sholine

Programmer
Feb 16, 2004
4
FR
Hello everybody !
I don't know how to count different values from a column. I axplain myself :
Here is my DB :

(PLACE & PEOPLE are strings, GAME is a SET of (game1, game2, game3)
PLACE PEOPLE GAME
Tokyo Mr A game1
Tokyo Mr B game1
N-Y Mr C game2
Paris Mr D game3
Tokyo Mr B game2
N-Y Mr C game3
Tokyo Mr B game1

Here is what I want :
PLACE PEOPLE NB_GAME1 NB_GAME2 NB_GAME3
Tokyo Mr A 1 0 0
Tokyo Mr B 2 1 0
N-Y Mr C 0 1 1
Paris Mr D 0 0 1

In fact, I want to explode the GAME column in a COUNT of each set it's composed of.

Thanks a lot for your response !!
 
select
place, people,
SUM(CASE WHEN Game = 'game1' THEN 1 ELSE 0) AS NB_GAME1,
SUM(CASE WHEN Game = 'game2' THEN 1 ELSE 0) AS NB_GAME2,
SUM(CASE WHEN Game = 'game3' THEN 1 ELSE 0) AS NB_GAME3
from
mytable
group by
place, people

Dieter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top