如何以每个记录与“上一个”记录连接的方式自行连接表?

时间:2021-03-22 04:05:59

I have a MS SQL table that contains stock data with the following columns: Id, Symbol, Date, Open, High, Low, Close.

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

I would like to self-join the table, so I can get a day-to-day % change for Close.

我想自己加入桌子,所以我可以获得关闭的日常%变化。

I must create a query that will join the table with itself in a way that every record contains also the data from the previous session (be aware, that I cannot use yesterday's date).

我必须创建一个查询,将表格与自身连接在一起,每个记录也包含来自前一个会话的数据(请注意,我不能使用昨天的日期)。

My idea is to do something like this:

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

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)

However I do not know if that's the correct/fastest way. What should I take into account when thinking about performance? (E.g. will putting UNIQUE index on a (Symbol, Date) pair improve performance?)

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

There will be around 100,000 new records every year in this table. I am using MS SQL Server 2008

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

7 个解决方案

#1


9  

One option is to use a recursive cte (if I'm understanding your requirements correctly):

一种选择是使用递归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 Fiddle Demo

SQL小提琴演示

If you need a running total for each symbol to use as the percentage change, then easy enough to add an additional column for that amount -- wasn't completely sure what your intentions were, so the above just divides the current closed amount by the previous closed amount.

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

#2


2  

Something like this w'd work in SQLite:

这样的东西在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)

Given SQLite is a simplest of a kind, maybe in MSSQL this will also work with minimal changes.

鉴于SQLite是最简单的一种,也许在MSSQL中,这也可以在最小的变化下工作。

#3


1  

You do something like this:

你做这样的事情:

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

If you change the last join to a left join you get a row for the first date for each symbol, not sure if you need that.

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

#4


0  

What you had is fine. I don't know if translating the sub-query into the join will help. However, you asked for it, so the way to do it might be to join the table to itself once more.

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

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

#5


0  

You could do something like this:

你可以这样做:

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

That way you limit your "today" results, if that's an option.

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

EDIT: The CTEs listed as other questions may work well, but I tend to be hesitant to use ROW_NUMBER when dealing with 100K rows or more. If the previous day may not always be yesterday, I tend to prefer to pull out the check for the previous day in its own query then use it for reference:

编辑:作为其他问题列出的CTE可能运行良好,但在处理100K行或更多行时,我倾向于使用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

#6


0  

You can use option with CTE and ROW_NUMBER ranking function

您可以使用CTE和ROW_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

For improving performance(avoiding sorting and RID Lookup) use this index

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

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

Simple demo on SQLFiddle

关于SQLFiddle的简单演示

#7


0  

Index on (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

#1


9  

One option is to use a recursive cte (if I'm understanding your requirements correctly):

一种选择是使用递归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 Fiddle Demo

SQL小提琴演示

If you need a running total for each symbol to use as the percentage change, then easy enough to add an additional column for that amount -- wasn't completely sure what your intentions were, so the above just divides the current closed amount by the previous closed amount.

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

#2


2  

Something like this w'd work in SQLite:

这样的东西在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)

Given SQLite is a simplest of a kind, maybe in MSSQL this will also work with minimal changes.

鉴于SQLite是最简单的一种,也许在MSSQL中,这也可以在最小的变化下工作。

#3


1  

You do something like this:

你做这样的事情:

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

If you change the last join to a left join you get a row for the first date for each symbol, not sure if you need that.

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

#4


0  

What you had is fine. I don't know if translating the sub-query into the join will help. However, you asked for it, so the way to do it might be to join the table to itself once more.

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

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

#5


0  

You could do something like this:

你可以这样做:

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

That way you limit your "today" results, if that's an option.

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

EDIT: The CTEs listed as other questions may work well, but I tend to be hesitant to use ROW_NUMBER when dealing with 100K rows or more. If the previous day may not always be yesterday, I tend to prefer to pull out the check for the previous day in its own query then use it for reference:

编辑:作为其他问题列出的CTE可能运行良好,但在处理100K行或更多行时,我倾向于使用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

#6


0  

You can use option with CTE and ROW_NUMBER ranking function

您可以使用CTE和ROW_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

For improving performance(avoiding sorting and RID Lookup) use this index

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

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

Simple demo on SQLFiddle

关于SQLFiddle的简单演示

#7


0  

Index on (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