一道sql面試題
阿新 • • 發佈:2020-08-18
已知使用者表t_user, uid int 使用者id
訂單表:t_order
oid int 訂單id
uid int 使用者id
otime date 訂單時間
Amount double 訂單金額
其中使用者表和訂單表是一對多的關係
需求:
結果集要求:
計算在2018年1月下過訂單,2月份沒有下過訂單的使用者在3月份的訂單金額分佈。
具體欄位如下:
uid,三月份訂單金額超過10的訂單數,3月份首次下單的金額,3月份最後一次下單的金額
測試資料如下:
create table t_order (oid int , uid int , otime date, oamount int )partitionedby (dt string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'; insert into table t_order partition(dt ='2018-01-01') values(1003,2,'2018-01-01',100); insert into table t_order partition(dt ='2018-01-02') values(1004,2,'2018-01-02',20); insert into table t_order partition(dt ='2018-01-02') values(1005,2,'2018-01-02',100); insertinto table t_order partition(dt ='2018-01-02') values(1006,4,'2018-01-02',30); insert into table t_order partition(dt ='2018-01-03') values(1007,1,'2018-01-03',130); insert into table t_order partition(dt ='2018-01-03') values(1008,2,'2018-01-03',5); insert into table t_order partition(dt ='2018-01-03') values(1009,2,'2018-01-03',5);
insert into table t_order partition(dt ='2018-02-01') values(1001,5,'2018-02-01',110); insert into table t_order partition(dt ='2018-02-01') values(1002,3,'2018-02-01',110); insert into table t_order partition(dt ='2018-02-03') values(1003,3,'2018-02-03',100); insert into table t_order partition(dt ='2018-02-03') values(1004,3,'2018-02-03',20); insert into table t_order partition(dt ='2018-02-04') values(1005,3,'2018-02-04',30); insert into table t_order partition(dt ='2018-02-04') values(1006,6,'2018-02-04',100); insert into table t_order partition(dt ='2018-02-04') values(1007,6,'2018-02-04',130); insert into table t_order partition(dt ='2018-03-01') values(1001,1,'2018-03-01',120); insert into table t_order partition(dt ='2018-03-03') values(1002,2,'2018-03-03',5); insert into table t_order partition(dt ='2018-03-03') values(1003,2,'2018-03-03',11); insert into table t_order partition(dt ='2018-03-03') values(1004,3,'2018-03-03',1); insert into table t_order partition(dt ='2018-03-04') values(1005,3,'2018-03-04',20); insert into table t_order partition(dt ='2018-03-04') values(1006,4,'2018-03-04',30); insert into table t_order partition(dt ='2018-03-04') values(1007,1,'2018-03-04',50);
實現:
select uid, sum(if(date_format(otime,'yyyy-MM')='2018-01',1,0)) as month1_order_cnt, sum(if(date_format(otime,'yyyy-MM')='2018-02',1,0)) as month2_order_cnt, sum(if(date_format(otime,'yyyy-MM')='2018-03' and oamount>10,oamount,0)) as month3_order_amount, sum(if(date_format(otime,'yyyy-MM')='2018-03' and rk=1,oamount,0)) as month3_first, sum(if(date_format(otime,'yyyy-MM')='2018-03' and rk=cnt,oamount,0)) as month3_last from( select count(*) over(partition by uid,date_format(otime,'yyyy-MM')) as cnt, row_number() over(partition by uid,date_format(otime,'yyyy-MM')) as rk, * from t_order ) as t group by uid having month1_order_cnt>0 and month2_order_cnt=0