我有一张带股票报价的表格
符号
问
投标
QuoteDateTime
使用SQL Server 2008,假设任何给定的60秒时间段我想在所有符号上选择引号,以便在该时间段内每秒都有一条记录。
问题不是每个符号都有相同的引号数 - 所以有些秒没有任何给定符号的引号记录。所以我想填补数据缺失的漏洞。因此,如果ORCL在第二个1,2,3,5,7处有引号,我希望结果集具有1,2,3,4,5,6,7 ......最多60秒(覆盖整个分钟) 。第4行中的值来自第3行
在这种情况下,我希望选择之前的引用并将其用于该特定秒。因此,每个符号都有连续记录,并为每个符号选择相同数量的记录。
我不确定在sql server中调用了什么,但是构建查询的任何帮助都很棒
对于输出我期望任何给定的60秒时间段。对于那些在60秒内有记录的符号,每个符号将有60个记录,每秒一个
Symbol询问出价QuoteDateTime
MSFT 26.00 27.00 2010-05-20 06:28:00
MSFT 26.01 27.02 2010-05-20 06:28:01
...
ORCL 26.00 27.00 2010-05-20 06:28:00
ORCL 26.01 27.02 2010-05-20 06:28:01
等
答案 0 :(得分:0)
这是一种方式。也可以使用三角形连接。不知道有没有其他选择?
DECLARE @startTime DATETIME = '2010-09-16 14:59:00.000';
WITH Times AS
(
SELECT @startTime AS T
UNION ALL
SELECT DATEADD(SECOND,1, T) FROM Times
WHERE T < DATEADD(MINUTE,1,@startTime)
),
Stocks AS
(
SELECT 'GOOG' AS Symbol
UNION ALL
SELECT 'MSFT'
),
Prices AS
(
SELECT 'GOOG' AS Symbol, 1 AS Ask, 1 AS Bid,
CAST('2010-09-16 14:59:02.000' AS DATETIME) AS QuoteDateTime
UNION ALL
SELECT 'GOOG' AS Symbol, 1 AS Ask, 1 AS Bid,
CAST('2010-09-16 14:59:02.000' AS DATETIME) AS QuoteDateTime
UNION ALL
SELECT 'GOOG' AS Symbol, 1 AS Ask, 1 AS Bid,
CAST('2010-09-16 14:59:02.000' AS DATETIME) AS QuoteDateTime
UNION ALL
SELECT 'MSFT' AS Symbol, 1 AS Ask, 1 AS Bid,
CAST('2010-09-01 12:00:00.000' AS DATETIME) AS QuoteDateTime
)
SELECT p.Symbol, p.Ask, p.Bid, p.QuoteDateTime
FROM Times t CROSS JOIN Stocks s
CROSS APPLY
(SELECT TOP 1 p.Symbol, p.Ask, p.Bid, p.QuoteDateTime
FROM Prices p
WHERE s.Symbol = p.Symbol AND p.QuoteDateTime <= t.T) p