T-SQL Multiple grouping

后端 未结 5 857
野性不改
野性不改 2021-01-13 13:46

I have follwing data :

Product Price   StartDate                   EndDate
Apples  4.9     2010-03-01 00:00:00.000     2010-03-01 00:00:00.000
Apples  4.9           


        
5条回答
  •  逝去的感伤
    2021-01-13 14:40

    SELECT  product, price, MIN(start_date), MAX(end_date)
    FROM    (
            SELECT  product, price, start_date, end_date,
                    ROW_NUMBER() OVER (PARTITION BY product ORDER BY startDate) rn1,
                    ROW_NUMBER() OVER (PARTITION BY product, price ORDER BY startDate) rn2
            FROM    mytable
            ) q
    GROUP BY
            product, price, rn2 - rn1
    ORDER BY
            product, MIN(start_date), price
    

提交回复
热议问题