一个数据工作者面试数据相关岗位,SQL查询语句是必不可少的笔试环节,今天云朵君给大家带来了某厂一道面试题,附上参考答案,希望能够帮到大家!
◎ 根据司机完单表求2017年7月1日-2017年7月31日,有过10天以上的完单并且总完单量在20单以上的司机id,司机姓名,司机完单天数、司机完单数 ◎ 根据司机信息表(driver_info)和司机汇总表(driver_collect)取出近2017.07.01-2017.07.31完单大于30单的司机姓名及电话
司机完单表 driver_daily
司机id | 司机名称 | 城市id | 城市名称 | 订单id | 年 | 月 | 日 |
---|---|---|---|---|---|---|---|
driver_id | driver_name | city_id | city_name | ordre_id | year | month | day |
111 | 王** | 32 | 厦门市 | 12233 | 2017 | 7 | 1 |
202 | 林** | 32 | 厦门市 | 32234 | 2017 | 9 | 1 |
司机信息表 driver_info
driver_id | driver_name | driver_phone |
---|---|---|
110 | 王** | 159*****4134 |
111 | 林** | 159*****7134 |
222 | 张** | 159*****8134 |
司机汇总表 driver_collect
driver_id | order_id | year | month | day |
---|---|---|---|---|
111 | 111 | 2017 | 7 | 1 |
222 | 112 | 2017 | 7 | 1 |
数据扩展
因为题目中给的数据样例比较少,因此给大家扩展了些数据,方便大家理解与练习,下图只截取部分数据,完整数据可以在文末查看。
司机完单表
司机汇总表
参考解答
※ 2017年7月1日-2017年7月31日,有过10天以上的完单并且总完单量在20单以上的司机id,司机姓名,司机完单天数、司机完单数
☆ 解析:
① 2017年7月1日-2017年7月31日 -- 可通过WHERE
筛选年是2017,月份是7。
② 司机完单天数、司机完单数 -- 先通过司机ID进行聚合,并对完单天数和完单量进行聚合求和。
③ 10天以上的完单并且总完单量在20单以上 -- 聚合后通过HAVING
筛选即可。
SELECT driver_id,driver_name,
COUNT(DISTINCT d_day)完单天数,
COUNT(DISTINCT order_id)完单数
FROM driver_daily
WHERE d_year=2017 AND d_month=7
GROUP BY driver_id
HAVING 完单天数 >=10 AND 完单数 >= 20;
☆ 结果:
driver_id | driver_name | 完单天数 | 完单数 |
---|---|---|---|
202 | 林** | 11 | 21 |
※ 近2017.07.01-2017.07.31完单大于30单的司机姓名及电话
☆ 解析:
① 完单在司机汇总表,司机姓名及电话在司机信息表,因此需要将两个表链接。
② 2017.07.01-2017.07.31 -- 可通过WHERE
筛选年是2017,月份是7。
③ 完单大于30单 -- 需要按照司机ID driver_id
聚合,将订单ID聚合后计数,再通过HAVING
筛选大于30单的数据。
SELECT driver_name,driver_phone,
COUNT(DISTINCT order_id)
FROM driver_info
LEFT JOIN driver_collect
ON driver_info.driver_id = driver_collect.driver_id
WHERE d_year=2017 AND d_month=7
GROUP BY driver_info.driver_id
HAVING COUNT(DISTINCT order_id) > 30;
☆ 结果:
driver_name | driver_phone | count(distinct order_id) |
---|---|---|
张** | 159****8134 | 31 |
建表与导数
为方便小伙伴们操作联系,数据库建表和导入数据代码给你贴出来了。
代码语言:javascript复制# create database STUDIO;
use STUDIO;
create table driver_daily(
driver_id varchar(10),
driver_name varchar(10),
city_id varchar(10),
city_name varchar(10),
order_id varchar(10),
d_year int,
d_month int,
d_day int
);
insert into driver_daily values
('111','王**','32','厦门市','12233',2017,7,1),
('111','王**','32','厦门市','12234',2017,7,1),
('111','王**','32','厦门市','12235',2017,7,1),
('111','王**','32','厦门市','12236',2017,7,1),
('111','王**','32','厦门市','12237',2017,7,1),
('111','王**','32','厦门市','12238',2017,7,1),
('111','王**','32','厦门市','12239',2017,7,1),
('111','王**','32','厦门市','12240',2017,7,1),
('111','王**','32','厦门市','12241',2017,7,1),
('111','王**','32','厦门市','12242',2017,7,1),
('111','王**','32','厦门市','12243',2017,7,1),
('111','王**','32','厦门市','12244',2017,7,1),
('111','王**','32','厦门市','12245',2017,7,1),
('111','王**','32','厦门市','12246',2017,7,1),
('111','王**','32','厦门市','12247',2017,7,1),
('111','王**','32','厦门市','12248',2017,7,1),
('111','王**','32','厦门市','12249',2017,7,1),
('111','王**','32','厦门市','12250',2017,7,1),
('111','王**','32','厦门市','12251',2017,7,1),
('111','王**','32','厦门市','12252',2017,7,1),
('202','林**','32','厦门市','32234',2017,7,1),
('202','林**','32','厦门市','32235',2017,7,1),
('202','林**','32','厦门市','32236',2017,7,2),
('202','林**','32','厦门市','32237',2017,7,2),
('202','林**','32','厦门市','32238',2017,7,3),
('202','林**','32','厦门市','32239',2017,7,3),
('202','林**','32','厦门市','32240',2017,7,4),
('202','林**','32','厦门市','32241',2017,7,4),
('202','林**','32','厦门市','32242',2017,7,5),
('202','林**','32','厦门市','32243',2017,7,5),
('202','林**','32','厦门市','32244',2017,7,6),
('202','林**','32','厦门市','32245',2017,7,6),
('202','林**','32','厦门市','32246',2017,7,7),
('202','林**','32','厦门市','32247',2017,7,7),
('202','林**','32','厦门市','32248',2017,7,7),
('202','林**','32','厦门市','32249',2017,7,8),
('202','林**','32','厦门市','32250',2017,7,8),
('202','林**','32','厦门市','32251',2017,7,8),
('202','林**','32','厦门市','32252',2017,7,9),
('202','林**','32','厦门市','32253',2017,7,9),
('202','林**','32','厦门市','32254',2017,7,10),
('202','林**','32','厦门市','32254',2017,7,11);
create table driver_info(
driver_id varchar(10),
driver_name varchar(10),
driver_phone varchar(20)
);
insert into driver_info values('110','王**','159****4134'),
('111','林**','159****7134'),
('222','张**','159****8134');
create table driver_collect(
driver_id varchar(10),
order_id varchar(10),
d_year int,
d_month int,
d_day int
);
insert into driver_collect values('111','111',2017,7,1),
('222','112',2017,7,1),
('222','113',2017,7,2),
('222','114',2017,7,3),
('222','115',2017,7,4),
('222','116',2017,7,5),
('222','117',2017,7,6),
('222','118',2017,7,7),
('222','119',2017,7,8),
('222','120',2017,7,9),
('222','121',2017,7,10),
('222','122',2017,7,11),
('222','123',2017,7,12),
('222','124',2017,7,13),
('222','125',2017,7,14),
('222','126',2017,7,15),
('222','127',2017,7,16),
('222','128',2017,7,17),
('222','129',2017,7,18),
('222','130',2017,7,19),
('222','131',2017,7,20),
('222','132',2017,7,21),
('222','133',2017,7,22),
('222','134',2017,7,23),
('222','135',2017,7,24),
('222','136',2017,7,25),
('222','137',2017,7,26),
('222','138',2017,7,27),
('222','139',2017,7,28),
('222','140',2017,7,29),
('222','141',2017,7,30),
('222','142',2017,7,31),
('222','143',2017,9,31);
select * from driver_daily;
select * from driver_info;
select * from driver_collect;