攜程筆試題3--2020-09-08
題目:在很多預測模型中,往往需要用到同一行為的不同周期匯總值作為特征。比如近1/7/15/30/60天購買筆數和金額。因此,怎么用簡潔的sql獲取這些特征是作為一個分析師必須要掌握的技能。
輸入描述:
訂單表edw_htl_order:
orderid bint comment (訂單id)
userid bigint comment(訂單id)
orderdate string comment (下單日期)
amount double comment(訂單金額)
輸出結果:用戶id,近一天的訂單數,近一天的訂單金額,近七天訂單數,近七天訂單金額,近15天訂單數,近15天訂單金額
說明:為了兼容各sql引擎,我們簡化約定近n天判斷如下:
近1天:【‘2020-07-15’,‘2020-07-16’】
近7天:【‘2020-07-09’,‘2020-07-16’】
近15天:【‘2020-07-01’,‘2020-07-16’】
樣例輸入:



樣例輸出:
輸入代碼:
1 select e1.userid,e1.cnt_1d,e1.amt_1d,e2.cnt_7d,e2.amt_7d,e3.cnt_15d,e3.amt_15d 2 from (select userid, count(orderdate) cnt_1d, sum(amount) amt_1d 3 from edw_htl_order 4 where orderdate between '2020-07-15' and '2020-07-16' 5 group by userid) as e1 6 inner join 7 (select userid, count(orderdate) cnt_7d, sum(amount) amt_7d 8 from edw_htl_order 9 where orderdate between '2020-07-09' and '2020-07-16' 10 group by userid) as e2 on e1.userid = e2.userid 11 inner join 12 (select userid, count(orderdate) cnt_15d, sum(amount) amt_15d 13 from edw_htl_order 14 where orderdate between '2020-07-01' and '2020-07-16' 15 group by userid) as e3 on e2.userid = e3.userid;
輸出結果:

浙公網安備 33010602011771號