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

Oracle SQL:基于列值的累积总计,sql,oracle,Sql,Oracle,我有两个数据表: 载有体育比赛中得分日期和时间的表格;及 表B列出了比赛日期以及主客场的球队编号 表 Date Time Team Points_scored ----------------------------------------- 20130818 1400 1 2 20130818 1402 1 3 20130818 1407 2 2 20130818 1410 2

我有两个数据表:

  • 载有体育比赛中得分日期和时间的表格;及
  • 表B列出了比赛日期以及主客场的球队编号

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

Date        Home Team   Away Team
-----------------------------------------------------
20130818    2           1
20130822    4           5
我想要的是一个查询,提供主客场球队每天的跑步总数,如下所示:

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
但我甚至不知道从哪里开始。有人有什么想法吗?我正在使用Oracle 11g

非常感谢

以下是创建脚本:

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;

这其中最难的部分不是累积和分析函数。它是使表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;
是SQL的把戏


顺便说一下,根据您的数据,
20130818
的最后一个值应该是
7
,而不是
6
(得2分)。

这其中最难的部分不是累积和分析函数。它是使表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;
是SQL的把戏


顺便说一下,根据您的数据,
20130818
的最后一个值应该是
7
,而不是
6
(得2分)。

很好的解决方案+回答得很好,但要给达莫留个条子。考虑一下一天可能会有一个以上的游戏。由于您将比赛日期存储为一个数字,请在末尾再添加一个数字,以防万一。很好,戈登,谢谢!工作完全符合要求。抱歉,所需的输出数据不正确。罗伯特,谢谢你提醒我一天中的两场比赛。解决得好,戈登+回答得很好,但要给达莫留个条子。考虑一下一天可能会有一个以上的游戏。由于您将比赛日期存储为一个数字,请在末尾再添加一个数字,以防万一。很好,戈登,谢谢!工作完全符合要求。抱歉,所需的输出数据不正确。罗伯特,谢谢你提醒我一天中的两场比赛。