知乎-数据分析师SQL刷题, 体验完爆牛客网-数据民工来取经儿
-- 我的答案
SELECT oi.userId,count(oi.userId) as order_times
FROM order_info as oi
group by oi.userId
order by order_times desc;
运行结果:
101 8
102 3
112 2
103 2
104 1
105 1
106 1
107 1
108 1
109 1
110 1
111 1
113 1
114 1
115 1
117 1
118 1
-- 网站所给答案
SELECT userid, count(*) as freq from order_info group by userid;
运行结果:
101 8
102 3
103 2
104 1
105 1
106 1
107 1
108 1
109 1
110 1
111 1
112 2
113 1
114 1
115 1
117 1
118 1
-- 我的答案
select user_freq.order_times as freq, count(user_freq.userId) as userNum
from (SELECT distinct oi.userId,count(oi.userId) as order_times
FROM order_info as oi group by oi.userId order by order_times desc) as user_freq
group by user_freq.order_times;
运行结果:
1 13
2 2
3 1
8 1
-- 网站所给答案
select t1.freq, count(*) as userNum from
(SELECT userid, count(*) as freq from order_info group by userid) t1
group by t1.freq
order by t1.freq;
运行结果:
1 13
2 2
3 1
8 1
-- 我的答案
select userId, max(payTime) as '最近消费时间'
from order_info as oi
group by oi.userId;
运行结果:
101 2019-06-02T21:21:00
102 2019-06-02T21:21:00
103 2019-06-01T23:23:00
104 2019-06-01T22:22:00
105 2019-06-01T22:22:00
106 2019-06-01T22:22:00
107 2019-06-01T22:22:00
108 2019-06-01T22:22:00
109 2019-06-01T22:22:00
110 2019-06-01T22:22:00
111 2019-06-01T22:22:00
112 2019-05-28T12:12:00
113 2019-05-27T14:14:00
114 2019-05-27T14:14:00
115 2019-05-27T14:14:00
117 2019-05-27T14:14:00
118 2019-05-27T14:14:00
-- 网站所给答案
SELECT userid, date(max(payTime)) as freq from order_info group by userid;
运行结果-通过date()函数转化为日期:
101 2019-06-02
102 2019-06-02
103 2019-06-01
104 2019-06-01
105 2019-06-01
106 2019-06-01
107 2019-06-01
108 2019-06-01
109 2019-06-01
110 2019-06-01
111 2019-06-01
112 2019-05-28
113 2019-05-27
114 2019-05-27
115 2019-05-27
117 2019-05-27
118 2019-05-27
-- 我的答案
select user_first_pay_day.firstPayDay,
count(user_first_pay_day.userId)as userNum
from (select oi.userId, date(min(oi.payTime)) as firstPayDay
from order_info as oi group by oi.userId) as user_first_pay_day
group by user_first_pay_day.firstPayDay;
运行结果:
2019-05-25 3
2019-05-27 6
2019-06-01 8
-- 网站所给答案:别名更精简
select date, count(*) as userNum
from (select userId,date(min(payTime)) as date from order_info
group by userId) t1
group by date;
运行结果:
2019-05-25 3
2019-05-27 6
2019-06-01 8
-- 我的答案
select user_hours.hours as hourNum,count(user_hours.userId) as userNum
from(select oi.userId,timestampdiff(hour,oi.createTime,oi.payTime)as hours
from order_info as oi)as user_hours
group by user_hours.hours;
运行结果:
2 1
3 5
4 17
5 4
6 1
-- 网站所给答案
select hourNum, count(*) as userNum
from(select userId,timestampdiff(hour, createTime, payTime) as hourNum
from order_info) t1
group by hourNum;
运行结果:
2 1
3 5
4 17
5 4
6 1
-- 我的答案:做错了
select oi.deskid, count(oi.orderid) as orderNum,count(oi.orderid)*di.num as deskNum
from order_info as oi
inner join desk_info as di on di.deskId = oi.deskid
group by oi.deskId;
运行结果-其实是错误答案,我求出来的是座位总数,偏离题意:
101 3 6
102 3 6
103 1 2
104 1 2
105 4 16
106 4 16
107 6 36
108 1 1
109 2 2
110 3 3
-- 我修改之后的答案
select oi.deskid,count(oi.orderId) as orderNum,
count(DISTINCT oi.userId) as userNum
from order_info as oi
group by oi.deskid
运行结果:
101 3 1
102 3 1
103 1 1
104 1 1
105 4 4
106 4 4
107 6 6
108 1 1
109 2 1
110 3 2
-- 网站索所给答案
select t1.deskId, count(orderId) as orderNum, count(distinct userId) as userNum
from desk_info t1
left join order_info t2 on t1.deskId=t2.deskId
group by t1.deskId
运行结果:
101 3 1
102 3 1
103 1 1
104 1 1
105 4 4
106 4 4
107 6 6
108 1 1
109 2 1
110 3 2
-- 我的答案
select di.num as seatNum, count(oi.orderid) as orderNum
from order_info as oi
inner join desk_info as di on di.deskId = oi.deskid
group by di.num;
运行结果:
1 6
2 8
4 8
6 6
-- 网站所给答案
select t1.num, count(orderId) as orderNum
from desk_info t1
left join order_info t2 on t1.deskId=t2.deskId
group by t1.num
运行结果:
1 6
2 8
4 8
6 6
-- 我的答案
select date(ucd.expireTime) as exDate,count(DISTINCT ucd.userId)as userNum
from user_coupon_detail as ucd
group by date(ucd.expireTime);
运行结果:
2019-06-24 3
2019-06-25 1
2019-06-26 7
2019-06-27 2
2019-06-28 1
2019-06-29 1
2019-07-01 11
2019-07-02 2
-- 网站所给答案
select date(expireTime) as expireDate,
count(distinct userId) as userNum
from user_coupon_detail
group by date(expireTime);
运行结果:
2019-06-24 3
2019-06-25 1
2019-06-26 7
2019-06-27 2
2019-06-28 1
2019-06-29 1
2019-07-01 11
2019-07-02 2
-- 我最开始的答案-错了
select oi.userId, (sum(p.price*od.num*ci.discount/100)) as totalMoney
from order_info as oi
inner join order_detail as od on od.orderid = oi.orderid
inner join user_coupon_detail as ucd on ucd.couponId = oi.couponId
inner join coupon_info as ci on ci.type = ucd.type
inner join product as p on p.productid = od.productid
group by oi.userId
101 107.36
102 58.08
103 60.72
104 22
105 16.72
106 16.72
107 24.64
108 42.24
109 39.6
110 48.4
111 35.2
112 65.12
113 22
114 22
115 24.64
117 24.64
118 17.6
-- 修改之后的答案:又错了
select oi.userId,
sum(case when oi.couponId>0 then p.price*od.num*ci.discount/100
else p.price*od.num end) as totalMoney
from order_info as oi
inner join order_detail as od on od.orderid = oi.orderid
inner join user_coupon_detail as ucd on ucd.couponId = oi.couponId
inner join coupon_info as ci on ci.type = ucd.type
inner join product as p on p.productid = od.productid
group by oi.userId
101 107.36
102 58.08
103 60.72
104 22
105 16.72
106 16.72
107 24.64
108 42.24
109 39.6
110 48.4
111 35.2
112 65.12
113 22
114 22
115 24.64
117 24.64
118 17.6
select oi.userId,
sum(case when oi.couponId>0 then p.price*od.num*ci.discount/100
else p.price*od.num end) as totalMoney
from order_info as oi
inner join order_detail as od on od.orderid = oi.orderid
left join user_coupon_detail as ucd on ucd.couponId = oi.couponId
left join coupon_info as ci on ci.type = ucd.type
left join product as p on p.productid = od.productid
group by oi.userId
运行结果:
101 216.36
102 58.08
103 60.72
104 22
105 16.72
106 16.72
107 24.64
108 42.24
109 39.6
110 48.4
111 35.2
112 65.12
113 22
114 22
115 24.64
117 24.64
118 17.6
-- 网站所给答案
select t1.userId,
sum(case when t1.couponId>0 then price*t4.discount/100 else price end) as totalPay
from order_info t1
left join order_detail t2 on t1.orderId=t2.orderId
left join user_coupon_detail t3 on t1.couponId=t3.couponId
left join coupon_info t4 on t3.type=t4.type
left join product t5 on t2.productid=t5.productid
group by t1.userId
运行结果:
101 216.36
102 58.08
103 60.72
104 22
105 16.72
106 16.72
107 24.64
108 42.24
109 39.6
110 48.4
111 35.2
112 65.12
113 22
114 22
115 24.64
117 24.64
118 17.6