SQL subtract two rows based on date and another column

前端 未结 2 1979
梦如初夏
梦如初夏 2020-12-09 06:48

I need to subtract two rows in MySQL by using the most recent date from the previous date:

Starting table:

Stock       Date          Price
GOOG              


        
相关标签:
2条回答
  • 2020-12-09 07:38

    What I've done was I get two separate queries to get each stock's maximum date and minimum date. Try this,

    SELECT  maxList.stock, 
            maxList.Date,
            (maxlist.Price - minlist.Price) totalPrice
    FROM
        (
            SELECT  a.*
            FROM    tableName a INNER JOIN
            (
                SELECT      Stock, MAX(date) maxDate
                FROM        tableName
                GROUP BY    Stock
            ) b ON  a.stock = b.stock AND
                    a.date = b.maxDate
        ) maxList INNER JOIN
        (
            SELECT  a.*
            FROM    tableName a INNER JOIN
            (
                SELECT      Stock, MIN(date) minDate
                FROM        tableName
                GROUP BY    Stock
            ) b ON  a.stock = b.stock AND
                    a.date = b.minDate
        ) minList ON maxList.stock = minList.stock
    

    SQLFiddle Demo

    UPDATE 1

    seeing your last sentence: Right now I just have two dates per company, but bonus upvotes if your query can handle any number of dates. What if you have records like this?

    FB          2012-05-20     40
    FB          2012-05-21     34
    FB          2012-05-22     42
    

    what would be its result?

    enter image description here

    0 讨论(0)
  • 2020-12-09 07:38

    I know this was answered already, but the problem caught my interest so I thought I'd give it a try.

    I assumed you might want to see the difference between any two consecutive dates.

    So for the following data:

    GOOG        2012-09-07  42.34
    GOOG        2012-09-06  44.56
    GOOG        2012-09-01  44.32
    FB          2012-09-07  17.82
    FB          2012-08-05  12.98
    

    You would get:

    GOOG        2012-09-07  2012-09-06  -2.22
    GOOG        2012-09-06  2012-09-01  0.24
    FB          2012-09-07  2012-08-05  4.84
    

    The query I wrote for this is:

    SELECT t1.Stock, t1.Date AS CurrentDate, oldDate.Date AS OldDate, (t1.Price - oldDate.Price) AS PriceChange
    FROM MP_StockTable t1
    JOIN (SELECT t2.Stock, t2.Date AS date1, (SELECT MAX(t3.Date) FROM MP_StockTable t3 WHERE t3.Date < t2.Date AND t3.Stock = t2.Stock GROUP BY t3.Stock) AS date2 
            FROM MP_StockTable t2) AS StockDates ON StockDates.Stock = t1.Stock AND StockDates.date1 = t1.Date
    JOIN MP_StockTable oldDate ON oldDate.Stock = t1.Stock AND oldDate.Date = StockDates.date2 
    

    The inline table basically calculates the closest previous date for each row. So for the data above, the generated table would like this:

    GOOG        2012-09-07  2012-09-06
    GOOG        2012-09-06  2012-09-01
    GOOG        2012-09-01  NULL
    FB          2012-09-07  2012-08-05
    FB          2012-08-05  NULL
    

    We then use the two dates from that table to get our two price points.

    NOTE: I was testing in SQLServer, so apologies if this needs some tweaks for mySql (it's been years since used mySql, so I've forgotten the differences).

    0 讨论(0)
提交回复
热议问题