Search code examples
google-bigquerysum

how to sum for each value and for each stage


For this table, i want to make addition of the points for each team in each stage of each season with bigquery

saison  |stage |team_home |team_away|home_goal|away_goal| home_point| away_point|   
---------------------------------------------------------------------------------
2002    |  1   |France    |Bresil   |2        |1        |  3        |0          |    
2002    |  1   |Italie    |Bresil   |4        |3        |  3        |0          |    
2002    |  1   |France    |Italie   |1        |1        |  1        |1          |    
2002    |  2   |Italie    |Bresil   |3        |4        |  0        |3          | 
2002    |  2   |France    |Italie   |1        |1        |  1        |1          |    
2002    |  2   |Italie    |Bresil   |3        |4        |  0        |3          | 
2002    |  3   |France    |Italie   |1        |1        |  1        |1          |    
2003    |  1   |Italie    |Bresil   |3        |4        |  0        |3          |    
2003    |  1   |France    |Italie   |1        |2        |  0        |3          |    
2003    |  1   |Bresil    |France   |0        |1        |  0        |3          |    
2003    |  2   |France    |Italie   |1        |2        |  0        |3          |    
2003    |  2   |Bresil    |France   |0        |1        |  0        |3          |    
2003    |  2   |France    |Italie   |1        |2        |  0        |3          |    
2003    |  3   |Italie    |France   |0        |1        |  0        |3          |    

I want this result :

saison  |stage |team      |team_point|   
--------------------------------------
2002    |  1   |France    |4          |    
2002    |  1   |Italie    |4          |    
2002    |  1   |Bresil    |0          |  
2002    |  2   |France    |5          |    
2002    |  2   |Italie    |5          |    
2002    |  2   |Bresil    |3          |  
2002    |  3   |France    |6          |    
2002    |  3   |Italie    |6          |    
2002    |  3   |Bresil    |3          | 
2003    |  1   |France    |3          |    
2003    |  1   |Italie    |3          |    
2003    |  1   |Bresil    |3          |  
2003    |  2   |France    |6          |    
2003    |  2   |Italie    |9          |    
2003    |  2   |Bresil    |3          |  
2003    |  3   |France    |9          |    
2003    |  3   |Italie    |9          |    
2003    |  3   |Bresil    |3          |  

I think to make unpivot and use an aggregation but i can't do it


Solution

  • Consider below query:

    SELECT saison, stage, p.team, SUM(SUM(p.point)) OVER (PARTITION by saison, team ORDER BY stage) team_point,
      FROM sample, UNNEST([STRUCT(team_home AS team, home_point AS point), (team_away, away_point)]) p
     GROUP BY 1, 2, 3
     ORDER BY 1, 2, 4 DESC;
    

    output is slightly different from your expected one : Thanks to @Saransh's kind explanation, output get more close to your expected output except one thing.

    (update)

    WITH team_points AS (
      SELECT saison, stage, p.team, SUM(SUM(p.point)) OVER (PARTITION by saison, team ORDER BY stage) team_point,
        FROM sample, UNNEST([STRUCT(team_home AS team, home_point AS point), (team_away, away_point)]) p
       GROUP BY 1, 2, 3
    )
    SELECT saison, stage, team, IFNULL(team_point, LAST_VALUE(team_point IGNORE NULLS) OVER w) AS team_point
      FROM UNNEST([2002, 2003]) saison, UNNEST([1, 2, 3]) stage, UNNEST(['France', 'Bresil', 'Italie']) team
      LEFT JOIN team_points p ON p.saison = saison AND p.stage = stage AND p.team = team
    WINDOW w AS (PARTITION BY saison, team ORDER BY stage)  
     ORDER BY 1, 2, 3;
    

    enter image description here