NOTE, I am asuming you're using mySQL, check the mySQl online docs for the use of the IF statement and the use of variables in SQLs.
I start with the data structure with contains an ID for the rows, a field with the season (1,2,...etc) and a field
for the res value be it W,D or L
select season, // select season so we can group by it and it will appear on each row
(@ws:="W"), // set variable ws to W for use in IF statement (ws stands for W symbol)
(@ds:="D"),(@ls:="L"), // do the same for variables ds and ls (D and L symbol)
sum(IF(res=@ws,1,res)) as W , // do if statement, if res field equals W symbol return 1 else return res which is
// a non numeric number and appears to be resolving to 0
// if you have problems try
// sum(IF(res=@ws,1,0)), if res=ws return 1 else return 0
// sum basically adds up all the return values for the field res on each row
sum(IF(res=@ds,1,res)) as D, // same as above
sum(IF(res=@ls,1,res)) as L // same as above
from md group by season; // group by season so each returned row has the data of each season
What it does is:
say we have the followin info
ID (row#), season, res
1,1,W
2,1,W
3,1,L
4,1,D
5,2,D
6,2,W
gets row 1, season is 1 so it falls into the 1 group, res is W so
sum(IF(res=@ws,1,res)) as W , returns 1, the first sum field (W)is incremented by 1
sum(IF(res=@ws,1,res)) as D , returns 0, the second sum field (D) is NOT incremente by 1
sum(IF(res=@ls,1,res)) as L , returns 0, the third sum field (L) is NOT incremente by 1
current column values season=1,W=1,D=0,L=0
gets row 2, season is 1 so it falls into the 1 group, res is W so
sum(IF(res=@ws,1,res)) as W , returns 1, the first sum field (W)is incremented by 1
sum(IF(res=@ws,1,res)) as D , returns 0, the second sum field (D) is NOT incremente by 1
sum(IF(res=@ls,1,res)) as L , returns 0, the third sum field (L) is NOT incremente by 1
current column values season=1,W=2,D=0,L=0
gets row 3, season is 1 so it falls into the 1 group, res is L so
sum(IF(res=@ws,1,res)) as W , returns 0, the first sum field (W)is NOT incremented by 1
sum(IF(res=@ws,1,res)) as D , returns 0, the second sum field (D) is NOT incremente by 1
sum(IF(res=@ls,1,res)) as L , returns 1, the third sum field (L) is incremente by 1
current column values season=1,W=2,D=0,L=1
gets row 4, season is 1 so it falls into the 1 group, res is D so
W + 0
D + 1
L + 0
gets row 5, season is 2 so it falls into the 2 group, res is D so
W = 0 // equal sign since we are not incrementing previous values, it is a new group for season=2, so columns are set to 0
D = 1 // first row we find increments D
L = 0
current column values season=1,W=2,D=0,L=1 // we are done with season 1
current column values season=2,W=0,D=0,L=1 // added a second row to sql results with season=2
gets row 6, season is 2 so it falls into the 2 group, res is W so
W + 1
D +0
L+0
current column values season=1,W=2,D=0,L=1
current column values season=2,W=1,D=0,L=1
we are done with all records so the returned table is
season W D L
1 2 0 1
2 1 0 1
Hope it helps
If you keep on having problems send me your data structure to look it up, in the previous sample I posted
the data structure way down
Saludos
Gerardo
gerardo@tasistro.com