Interview question:How to get last 3 month aggregation at column level?

前端 未结 3 1013
执笔经年
执笔经年 2021-01-16 11:39

This is the question i was being asked at Apple onsite interview and it blew my mind. Data is like this:

orderdate,unit_of_phone_sale

20190806,3000

2019070         


        
3条回答
  •  囚心锁ツ
    2021-01-16 12:10

    You can use this query:

    select a.order_month, a.unit_of_phone_sale, 
    LEAD(unit_of_phone_sale, 1, 0) OVER (ORDER BY rownum) AS M_1,
    LEAD(unit_of_phone_sale, 2, 0) OVER (ORDER BY rownum) AS M_2,
    LEAD(unit_of_phone_sale, 3, 0) OVER (ORDER BY rownum) AS M_3
    from (
    select TO_CHAR(orderdate, 'YYYYMM') order_month,
    unit_of_phone_sale,
    rownum
    from Y
    order by order_month desc) a
    

提交回复
热议问题