Damo
Damo

Reputation: 25

Oracle SQL: cumulative totals based on column values

I have a two tables of data:

TableA

Date        Time    Team    Points_scored
-----------------------------------------
20130818    1400    1       2
20130818    1402    1       3
20130818    1407    2       2
20130818    1410    2       3
20130818    1412    1       2
20130822    1550    4       2
20130822    1552    5       3
20130822    1553    5       2
20130822    1555    5       3
20130822    1559    4       2

TableB

Date        Home Team   Away Team
-----------------------------------------------------
20130818    2           1
20130822    4           5

What I would like is a query that provides running totals for both the home and away teams of each day, like this:

Date        Time    Home_score    Away_score
20130818    1400    0             2
20130818    1402    0             5
20130818    1407    2             5
20130818    1410    5             5
20130818    1412    5             6
20130822    1550    2             0
20130822    1552    2             3
20130822    1553    2             5
20130822    1555    2             8
20130822    1559    4             8

But I'm not sure even where to start. Does anyone have any ideas? I'm using Oracle 11g.

Thanks very much.

Here is the create script:

create table tablea (
    match_date            number,
    time            number,
    team            number,
    points_scored   number);

create table tableb (
    match_date        number,
    home_team   number,
    away_team   number);

insert into tablea values (20130818,1400,1,2);
insert into tablea values (20130818,1402,1,3);
insert into tablea values (20130818,1407,2,2);
insert into tablea values (20130818,1410,2,3);
insert into tablea values (20130818,1412,1,2);
insert into tablea values (20130822,1550,4,2);
insert into tablea values (20130822,1552,5,3);
insert into tablea values (20130822,1553,5,2);
insert into tablea values (20130822,1555,5,3);
insert into tablea values (20130822,1559,4,2);

insert into tableb values (20130818,2,1);
insert into tableb values (20130822,4,5);

commit;

Upvotes: 1

Views: 2111

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269633

The hard part of this isn't the cumulative sum analytic function. It is getting the join between table a and table b right.

select b.match_date, a.time,
       (case when a.team = b.home_team then a.points_scored else 0 end) as home_points,
       (case when a.team = b.away_team then a.points_scored else 0 end) as away_points,
       sum(case when a.team = b.home_team then a.points_scored else 0 end) over (partition by a.match_date order by a.time) as cum_home_points,
       sum(case when a.team = b.away_team then a.points_scored else 0 end) over (partition by a.match_date order by a.time) as cum_away_points
from TableB b join
     TableA a
     on a.team in (b.home_team, b.away_team) and b.match_date = a.match_date;

Here is the SQL Fiddle.

By the way, according to your data, the last value for 20130818 should be 7 and not 6 (2 points are scored).

Upvotes: 4

Related Questions