复杂的SQL Server查询 - 对上一篇文章的扩展

时间:2014-04-28 15:02:54

标签: sql sql-server

这是此处问题的扩展:My first complex SQL queries

我现在想删除'WHERE MatchID = 9000'条件,并返回显示第一行数据(如前所述)的结果表,但是对于每个单独的MatchID。结果数据应如下所示:

MatchID WhenOddsTaken           MarketName  Outcome     Odds    NumberOfBets    VolumeMatched   InPlay
9000    2011-09-17 14:01:02.000 Match Odds  The Draw    3.95    2               94.28           1
9001    2011-09-17 14:04:38.000 Match Odds  The Draw    4.40    179             17707.36        0
9002    2011-09-17 14:03:55.000 Match Odds  The Draw    3.50    12              530.99          1
9003    2011-09-17 14:02:18.000 Match Odds  The Draw    3.50    6               99.55           1
9004    2011-09-18 14:02:12.000 Match Odds  The Draw    3.85    581             53612.29        0

以下是我正在使用的表格中的一些示例数据:

表名'数据':

MatchID OddsFirstTimeTaken      OddsLastTimeTaken       OddsTimeTaken   MarketName  Outcome     Odds    NumberOfBets    VolumeMatched   InPlay
9000    2011-09-17 14:01:24.000 2011-09-17 14:07:52.000 NULL            Match Odds  The Draw    3.80    69              3988.24         1
9000    2011-09-17 14:00:29.000 2011-09-17 14:07:25.000 NULL            Match Odds  The Draw    3.85    53              693.46          1
9000    2011-09-17 14:00:17.000 2011-09-17 14:04:05.000 NULL            Match Odds  The Draw    3.90    33              628.26          1
9000    2011-09-17 14:01:02.000 2011-09-17 14:01:02.000 NULL            Match Odds  The Draw    3.95    2               94.28           1
9000    2011-09-17 14:00:24.000 2011-09-17 14:00:24.000 NULL            Match Odds  The Draw    4.10    2               2.70            1
9001    2011-09-10 14:21:06.000 2011-09-17 13:58:02.000 NULL            Match Odds  The Draw    4.30    137             14295.24        0
9001    2011-09-10 14:29:43.000 2011-09-10 14:29:43.000 NULL            Match Odds  The Draw    3.85    2               16.30           0
9001    2011-09-10 16:26:20.000 2011-09-17 14:23:00.000 NULL            Match Odds  The Draw    4.00    74              1519.93         0
9001    2011-09-10 19:45:24.000 2011-09-17 14:21:10.000 NULL            Match Odds  The Draw    4.20    313             9887.68         0
9001    2011-09-10 20:47:38.000 2011-09-17 14:04:38.000 NULL            Match Odds  The Draw    4.40    179             17707.36        0
9002    2011-09-09 22:34:27.000 2011-09-17 14:00:01.000 NULL            Match Odds  The Draw    3.45    83              4257.78         0
9002    2011-09-09 22:34:27.000 2011-09-17 14:04:01.000 NULL            Match Odds  The Draw    3.40    538             52381.26        0
9002    2011-09-17 14:01:44.000 2011-09-17 14:07:23.000 NULL            Match Odds  The Draw    3.40    37              1763.60         1
9002    2011-09-17 14:02:16.000 2011-09-17 14:05:47.000 NULL            Match Odds  The Draw    3.45    30              557.36          1
9002    2011-09-17 14:02:33.000 2011-09-17 14:03:55.000 NULL            Match Odds  The Draw    3.50    12              530.99          1
9003    2011-09-11 08:40:39.000 2011-09-16 21:38:09.000 NULL            Match Odds  The Draw    3.60    66              2016.82         0
9003    2011-09-11 14:38:38.000 2011-09-11 14:38:38.000 NULL            Match Odds  The Draw    3.75    2               3.82            0
9003    2011-09-12 10:53:16.000 2011-09-16 05:15:40.000 NULL            Match Odds  The Draw    3.65    56              624.66          0
9003    2011-09-12 18:19:07.000 2011-09-15 19:13:17.000 NULL            Match Odds  The Draw    3.70    18              200.42          0
9003    2011-09-16 11:04:06.000 2011-09-17 14:02:18.000 NULL            Match Odds  The Draw    3.50    483             17996.59        0
9004    2011-09-11 10:38:02.000 2011-09-18 13:39:09.000 NULL            Match Odds  The Draw    4.00    255             11327.20        0
9004    2011-09-11 11:25:38.000 2011-09-11 11:59:46.000 NULL            Match Odds  The Draw    4.60    5               138.00          0
9004    2011-09-11 11:25:38.000 2011-09-11 11:27:05.000 NULL            Match Odds  The Draw    4.70    3               120.96          0
9004    2011-09-13 06:43:56.000 2011-09-18 14:02:12.000 NULL            Match Odds  The Draw    3.85    581             53612.29        0
9004    2011-09-13 09:57:24.000 2011-09-18 14:12:17.000 NULL            Match Odds  The Draw    3.80    190             21690.59        0

表名'匹配':

MatchID CompetitionID   DateKickOff             TimeKickOff         TeamIDHome  TeamIDAway  ScoreHome   ScoreAway
9000    1               2011-09-17 00:00:00.000 13:59:00.0000000    25          13          1           2
9001    1               2011-09-17 00:00:00.000 14:02:00.0000000    6           43          3           1
9002    1               2011-09-17 00:00:00.000 14:01:00.0000000    17          19          3           0
9003    1               2011-09-17 00:00:00.000 14:00:00.0000000    68          39          0           3
9004    1               2011-09-18 00:00:00.000 14:00:00.0000000    7           10          2           2

希望这是足够的数据和信息。如果需要更多,请告诉我。感谢

1 个答案:

答案 0 :(得分:1)

with x as (
    SELECT MatchID, DateTimeKO = DateKickOff + CAST(TimeKickOff AS DATETIME), ScoreHome, ScoreAway
    FROM Match
),
y as (
    SELECT MatchID, OddsFirstTimeTaken AS WhenOddsTaken, MarketName, Outcome, Odds, NumberOfBets, VolumeMatched, InPlay
    FROM Data as D
    WHERE MarketName = 'Match Odds' AND Outcome = 'The Draw'
    UNION ALL
    SELECT MatchID, OddsLastTimeTaken AS WhenOddsTaken, MarketName, Outcome, Odds, NumberOfBets, VolumeMatched, InPlay
    FROM Data as D
    WHERE MarketName = 'Match Odds' AND Outcome = 'The Draw'
),
z as (
    select y.MatchID, WhenOddsTaken, MarketName, Outcome, Odds, NumberOfBets, VolumeMatched, InPlay, ScoreHome, ScoreAway,
        row_number() over (partition by x.MatchID order by WhenOddsTaken asc) as rn
    from y
    inner join x on y.MatchID = x.MatchID
    where WhenOddsTaken > dateadd(minute, 2, DateTimeKO)
)
select *,
    Result = case 
        when ScoreHome > ScoreAway then 'Home Win'
        when ScoreHome = ScoreAway then 'The Draw'
        when ScoreHome < ScoreAway then 'Away Win'
    end
from z
where rn = 1
order by matchid