sql - row counter with condition in two different columns -


i have following tables sport results (e.g. football):

tblgoals (rowid, gamerowidm playerrowid, teamrowid, goalminute)

rowid | gamerowid | playerrowid | teamrowid | goalminute -------------------------------------------------------- 1     | 1         | 1           | 1         | 25 2     | 1         | 2           | 2         | 45 3     | 1         | 3           | 1         | 66 

tblplayers (rowid, playername)

rowid | playername ------------------ 1     | john snow 2     | frank underwood 3     | jack bauer 

tblgames (rowid, teamhomerowid, teamguestrowid)

rowid | teamhomerowid | teamguestrowid | gamedate --------------------------------------------------- 1     | 1             | 2              | 2015-01-01 

now want list of goals. list should this:

goalminute | playername      | goalshome | goalsguest ----------------------------------------------------- 25         | john snow       | 1         | 0 45         | frank underwood | 1         | 1 66         | jack bauer      | 2         | 1 

goalshome , goalsguest should counter of shot goals team. e.g. if check last row, result 2:1 home team. list of goals, used statement:

select  t_gol.goalminute,         t_ply.playername,         case when             t_gol.teamrowid = t_gam.teamhomerowid              row_number()  on (partition t_gam.teamhomerowid order t_gam.teamhomerowid)         end goalshome,         case when             t_gol.teamrowid = t_gam.teamguestrowid              row_number()  on (partition t_gam.teamguestrowid order t_gam.teamguestrowid)         end goalsguest    dbo.tblgoalsfussball t_gol left join dbo.tblplayersfussball t_ply on (t_ply.rowid = t_gol.playerrowid) left join dbo.tblgames t_gam on (t_gam.rowid = t_gol.gamerowid)   t_gol.gamerowid = @match_row 

but here:

goalminute | playername      | goalshome | goalsguest ----------------------------------------------------- 25         | john snow       | 1         | null 45         | frank underwood | null      | 2 66         | jack bauer      | 3         | null 

maybe row_number() wrong approach?

i running total using sum() windowed aggregate function over ... clause, works in sql server 2012+.

select      g.rowid, g.gamedate, t.goalminute, p.playername,      goalshome = coalesce(sum(case when teamrowid = g.teamhomerowid 1 end) on (partition gamerowid order goalminute),0),     goalsguest = coalesce(sum(case when teamrowid = g.teamguestrowid 1 end) on (partition gamerowid order goalminute),0)  tblgoals t join tblplayers p on t.playerrowid = p.rowid join tblgames g on t.gamerowid = g.rowid order t.gamerowid, t.goalminute 

another approach (that works in older versions) use self-join , sum rows lower goalminutes. ease of reading i've used common table expression split goals 2 columns home , guest team:

;with t (     select         g.goalminute, g.playerrowid, g.gamerowid,         case when teamrowid = ga.teamhomerowid 1 end homegoals,        case when teamrowid = ga.teamguestrowid 1 end guestgoals     tblgoals g     join tblgames ga on g.gamerowid = ga.rowid )  select      g.rowid, g.gamedate, t.goalminute, p.playername,      goalshome  = (select sum(coalesce(homegoals,0)) t t2 t2.goalminute <= t.goalminute , t2.gamerowid = t.gamerowid),     goalsguest = (select sum(coalesce(guestgoals,0)) t t2 t2.goalminute <= t.goalminute , t2.gamerowid = t.gamerowid) t join tblplayers p on t.playerrowid = p.rowid join tblgames g on t.gamerowid = g.rowid order t.gamerowid, t.goalminute 

the cte isn't necessary though, use derived table

sample sql fiddle


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -