简体   繁体   English

Oracle SQL:基于列值的累计总计

[英]Oracle SQL: cumulative totals based on column values

I have a two tables of data: 我有两个数据表:

  • TableA which holds the date and time of scores made during a sports match; 表A包含体育比赛期间得分的日期和时间; and
  • TableB which holds the date of a match and which team numbers were the home and away teams 表B包含比赛日期以及哪些球队号码是主队和客队

TableA 表A

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 表B

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. 我正在使用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;

The hard part of this isn't the cumulative sum analytic function. 其中最难的部分不是累积和解析函数。 It is getting the join between table a and table b right. 它正在使表a和表b之间的连接正确。

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. 是SQL小提琴。

By the way, according to your data, the last value for 20130818 should be 7 and not 6 (2 points are scored). 顺便说一句,根据您的数据, 20130818的最后一个值应为7而不是6 (得分为2分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM