10

我有一个 MS SQL 表,其中包含具有以下列的股票数据:Id, Symbol, Date, Open, High, Low, Close.

我想自行加入表格,这样我就可以获得Close.

我必须创建一个查询,该查询将以每条记录还包含上一个会话的数据的方式将表与自身连接起来(请注意,我不能使用昨天的日期)。

我的想法是做这样的事情:

select * from quotes t1
inner join quotes t2
on t1.symbol = t2.symbol and
t2.date = (select max(date) from quotes where symbol = t1.symbol and date < t1.date)

但是我不知道这是否是正确/最快的方式。在考虑性能时我应该考虑什么?(例如,将 UNIQUE 索引放在 (Symbol, Date) 对上会提高性能吗?)

该表中每年将有大约 100,000 条新记录。我正在使用 MS SQL Server 2008

4

7 回答 7

9

一种选择是使用递归 cte (如果我正确理解您的要求):

WITH RNCTE AS (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY symbol ORDER BY date) rn
        FROM quotes
  ),
CTE AS (
  SELECT symbol, date, rn, cast(0 as decimal(10,2)) perc, closed
  FROM RNCTE
  WHERE rn = 1
  UNION ALL
  SELECT r.symbol, r.date, r.rn, cast(c.closed/r.closed as decimal(10,2)) perc, r.closed
  FROM CTE c 
    JOIN RNCTE r on c.symbol = r.symbol AND c.rn+1 = r.rn
  )
SELECT * FROM CTE
ORDER BY symbol, date

SQL 小提琴演示

如果您需要每个符号的运行总计用作百分比变化,那么很容易为该金额添加一个额外的列 - 不完全确定您的意图是什么,所以上面只是将当前关闭的金额除以之前的平仓金额。

于 2013-03-20T15:23:13.937 回答
5

像这样的东西可以在 SQLite 中工作:

SELECT ..
FROM quotes t1, quotes t2
WHERE t1.symbol = t2.symbol
    AND t1.date < t2.date
GROUP BY t2.ID
    HAVING t2.date = MIN(t2.date)

鉴于 SQLite 是最简单的一种,也许在 MSSQL 中,这也可以通过最小的更改来工作。

于 2014-11-13T13:55:24.787 回答
2

索引(symbol, date)

SELECT *
FROM quotes q_curr
CROSS APPLY (
  SELECT TOP(1) *
  FROM quotes
  WHERE symbol = q_curr.symbol
    AND date < q_curr.date
  ORDER BY date DESC
) q_prev
于 2015-06-26T15:59:56.630 回答
1

您可以将选项与CTEROW_NUMBER排名功能一起使用

 ;WITH cte AS
 (
  SELECT symbol, date, [Open], [High], [Low], [Close],
         ROW_NUMBER() OVER(PARTITION BY symbol ORDER BY date) AS Id
  FROM quotes
  )
  SELECT c1.Id, c1.symbol, c1.date, c1.[Open], c1.[High], c1.[Low], c1.[Close], 
         ISNULL(c2.[Close] / c1.[Close], 0) AS perc
  FROM cte c1 LEFT JOIN cte c2 ON c1.symbol = c2.symbol AND c1.Id = c2.Id + 1
  ORDER BY c1.symbol, c1.date

为了提高性能(避免排序和 RID 查找)使用这个索引

CREATE INDEX ix_symbol$date_quotes ON quotes(symbol, date) INCLUDE([Open], [High], [Low], [Close])

SQLFiddle上的简单演示

于 2013-03-20T15:29:17.070 回答
1

你做这样的事情:

with OrderedQuotes as
(
    select 
        row_number() over(order by Symbol, Date) RowNum, 
        ID, 
        Symbol, 
        Date, 
        Open, 
        High, 
        Low, 
        Close
      from Quotes
)
select
    a.Symbol,
    a.Date,
    a.Open,
    a.High,
    a.Low,
    a.Close,
    a.Date PrevDate,
    a.Open PrevOpen,
    a.High PrevHigh,
    a.Low PrevLow,
    a.Close PrevClose,

    b.Close-a.Close/a.Close PctChange

  from OrderedQuotes a
  join OrderedQuotes b on a.Symbol = b.Symbol and a.RowNum = b.RowNum + 1

如果将最后一个连接更改为左连接,则每个符号的第一个日期都会出现一行,不确定是否需要。

于 2013-03-20T15:33:32.993 回答
0

你可以这样做:

DECLARE @Today DATETIME
SELECT @Today = DATEADD(DAY, 0, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP))

;WITH today AS
(
    SELECT  Id ,
            Symbol ,
            Date ,
            [OPEN] ,
            High ,
            LOW ,
            [CLOSE],
            DATEADD(DAY, -1, Date) AS yesterday 
    FROM quotes
    WHERE date = @today
)
SELECT *
FROM today
LEFT JOIN quotes yesterday ON today.Symbol = yesterday.Symbol
    AND today.yesterday = yesterday.Date

这样你就可以限制你的“今天”结果,如果这是一个选项的话。

编辑:作为其他问题列出的 CTE 可能效果很好,但在处理 10 万行或更多行时,我倾向于犹豫使用 ROW_NUMBER。如果前一天可能并不总是昨天,我倾向于在自己的查询中提取前一天的支票,然后将其用作参考:

DECLARE @Today DATETIME, @PreviousDay DATETIME
SELECT @Today = DATEADD(DAY, 0, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP));
SELECT @PreviousDay = MAX(Date) FROM quotes  WHERE Date < @Today;
WITH today AS
(
    SELECT  Id ,
            Symbol ,
            Date ,
            [OPEN] ,
            High ,
            LOW ,
            [CLOSE]
    FROM quotes 
    WHERE date = @today
)
SELECT *
FROM today
LEFT JOIN quotes AS previousday
    ON today.Symbol = previousday.Symbol
    AND previousday.Date = @PreviousDay
于 2013-03-20T15:20:32.487 回答
0

你所拥有的很好。我不知道将子查询翻译成连接是否会有所帮助。但是,您要求它,所以这样做的方法可能是再次将表连接到自身。

select *
from quotes t1
inner join quotes t2
   on t1.symbol = t2.symbol and t1.date > t2.date
left outer join quotes t3
   on t2.symbol = t3.symbol and t2.date > t3.date
where t3.date is null
于 2013-03-20T15:25:26.373 回答