How to self-join table in a way that every record is joined with the “previous” record?

后端 未结 7 1271
清歌不尽
清歌不尽 2021-01-02 06:55

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

I would like to self-join the table

7条回答
  •  抹茶落季
    2021-01-02 07:10

    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
    

提交回复
热议问题