我的第一个复杂的SQL查询

时间:2014-04-25 11:36:17

标签: sql sql-server

我一直在编写一个C#程序,用于从导入的.csv数据文件在SQL Server中创建数据库。我现在已经使数据库正常工作,需要对它进行一些相当复杂(对我而言)的查询。对于我目前正在处理的问题,我有这些问题:

SELECT MatchID, DateTimeKO = DateKickOff + CAST(TimeKickOff AS DATETIME)
FROM Match
WHERE MatchID = 9000

返回:

MatchID DateTimeKO
9000    2011-09-17 14:00:00.000

(SELECT MatchID, OddsFirstTimeTaken AS WhenOddsTaken, MarketName, Outcome, Odds, NumberOfBets, VolumeMatched,InPlay
FROM Data as D
WHERE MatchID = 9000  AND 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 MatchID = 9000 AND MarketName = 'Match Odds' AND Outcome = 'The Draw'
)ORDER BY WhenOddsTaken

返回:

MatchID WhenOddsTaken           MarketName  Outcome     Odds    NumberOfBets    VolumeMatched   InPlay
9000    2011-09-17 13:58:17.000 Match Odds  The Draw    3.80    243             15997.30        0
9000    2011-09-17 13:59:14.000 Match Odds  The Draw    3.90    39              1923.44         0
9000    2011-09-17 13:59:31.000 Match Odds  The Draw    3.95    8               209.12          0
9000    2011-09-17 13:59:44.000 Match Odds  The Draw    3.85    106             5740.90         0
9000    2011-09-17 14:00:17.000 Match Odds  The Draw    3.90    33              628.26          1
9000    2011-09-17 14:00:24.000 Match Odds  The Draw    4.10    2               2.70            1
9000    2011-09-17 14:00:29.000 Match Odds  The Draw    3.85    53              693.46          1
9000    2011-09-17 14:01:02.000 Match Odds  The Draw    3.95    2               94.28           1
9000    2011-09-17 14:01:24.000 Match Odds  The Draw    3.80    69              3988.24         1
9000    2011-09-17 14:04:05.000 Match Odds  The Draw    3.90    33              628.26          1
9000    2011-09-17 14:05:22.000 Match Odds  The Draw    3.75    20              1873.80         1

我现在要做的是添加另一个where条件,该条件从第二个查询返回数据,仅当'WhenOddsTaken'日期时间大于第一个查询中'DateTimeKO'的值时。即它应该返回:

MatchID WhenOddsTaken           MarketName  Outcome     Odds    NumberOfBets    VolumeMatched   InPlay
9000    2011-09-17 14:00:17.000 Match Odds  The Draw    3.90    33              628.26          1
9000    2011-09-17 14:00:24.000 Match Odds  The Draw    4.10    2               2.70            1
9000    2011-09-17 14:00:29.000 Match Odds  The Draw    3.85    53              693.46          1
9000    2011-09-17 14:01:02.000 Match Odds  The Draw    3.95    2               94.28           1
9000    2011-09-17 14:01:24.000 Match Odds  The Draw    3.80    69              3988.24         1
9000    2011-09-17 14:04:05.000 Match Odds  The Draw    3.90    33              628.26          1
9000    2011-09-17 14:05:22.000 Match Odds  The Draw    3.75    20              1873.80         1

除此之外,我想要进一步查询以返回在DateTimeKO之后的某个时间发生的第一行数据,例如DateTimeKO加2分钟(2011-09-17 14:02:00.000之后的第一行)将返回:

MatchID WhenOddsTaken           MarketName  Outcome     Odds    NumberOfBets    VolumeMatched   InPlay
9000    2011-09-17 14:04:05.000 Match Odds  The Draw    3.90    33              628.26          1

我将不胜感激任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

没有数据可以运行它,但这些内容有以下几点:

with x as (
    SELECT MatchID, DateTimeKO = DateKickOff + CAST(TimeKickOff AS DATETIME)
    FROM Match
    WHERE MatchID = 9000
),
y as (
    SELECT MatchID, OddsFirstTimeTaken AS WhenOddsTaken, MarketName, Outcome, Odds, NumberOfBets, VolumeMatched,InPlay
    FROM Data as D
    WHERE MatchID = 9000  AND 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 MatchID = 9000 AND MarketName = 'Match Odds' AND Outcome = 'The Draw'
)
select top (1) y.MatchID, WhenOddsTaken, MarketName, Outcome, Odds, NumberOfBets, VolumeMatched, InPlay
from y
inner join x on y.MatchID = x.MatchID
where WhenOddsTaken > dateadd(minute, 2, DateTimeKO)
order by WhenOddsTaken asc