Task06-综合练习(上)
本笔记为阿里云天池龙珠计划SQL训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql
综合练习
练习题6.1
请使用A股上市公司季度营收预测数据集《Income Statement.xls》和《Company Operating.xlsx》和《Market Data.xlsx》,以Market Data为主表,将三张表中的TICKER_SYMBOL为600383和600048的信息合并在一起。只需要显示以下字段。
表名 | 字段名 |
---|---|
Income Statement | TICKER_SYMBOL |
Income Statement | END_DATE |
Income Statement | T_REVENUE |
Income Statement | T_COGS |
Income Statement | N_INCOME |
Market Data | TICKER_SYMBOL |
Market Data | END_DATE |
Market Data | CLOSE_PRICE |
Company Operating | TICKER_SYMBOL |
Company Operating | INDIC_NAME_EN |
Company Operating | END_DATE |
Company Operating | VALUE |
代码如下:
select m.*,i.t_revenue,i.t_cogs,i.n_income,c.INDIC_NAME_EN,c.value
from
(select ticker_symbol,end_date,close_price from `market data`
where ticker_symbol in ('600383','600048')) as m
left join
(select ticker_symbol, end_date,t_revenue,t_cogs,n_income from `income statement`
where ticker_symbol in ('600383','600048')) as i
on m.ticker_symbol=i.ticker_symbol and m.end_date=i.end_date
left join
(select ticker_symbol,INDIC_NAME_EN,END_DATE,VALUE from `company operating`
where ticker_symbol in (600383,600048)) as c
on m.ticker_symbol=c.ticker_symbol and m.end_date=c.end_date;
结果如下图所示(仅部分):
练习题6.2
找出 pH=3.03的所有红葡萄酒,然后,对其 citric acid 进行中式排名(相同排名的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”)。
注意,当表名或者列名是由两个独立单词组成的时候,要用``括起来
下载的文件里winequality_red用的不是下划线而是-,表名无效,需要修改
代码如下:
select PH,`citric acid`, dense_rank() over(order by `citric acid`) as ranking
from `winequality_red`
where PH=3.03;
结果如下:
练习题6.3
试分别找出在2016年7月期间,发放优惠券总金额最多和发放优惠券张数最多的商家。
这里只考虑满减的金额,不考虑打几折的优惠券。
- 发放优惠券金额最多的商家,discount_rate 30:5表示满30-5
select merchant_id,sum(substring_index(discount_rate,':',-1)) as discount
from ccf_offline_stage1_test_revised
where date_format(date_received,'%Y%m')=201607
group by Merchant_id
order by discount desc
limit 1;
结果如下:
- 发放优惠券张数最多的商家
select merchant_id,count(Merchant_id) as cnt
from ccf_offline_stage1_test_revised
where date_format(date_received,'%Y%m')=201607
group by Merchant_id
order by cnt desc
limit 1;
结果如下:
练习题6.4
请计算全社会用电量:第一产业:当月值在2015年用电最高峰是发生在哪月?并且相比去年同期增长/减少了多少个百分比?
- 2015年最高
select period_date,data_value
from `macro industry`
where year(PERIOD_DATE)=2015 and indic_id=2020101522
order by data_value desc
limit 1;
- 去年最高
select period_date,data_value
from `macro industry`
where year(PERIOD_DATE)=2014 and indic_id=2020101522
group by period_date;
结果有多日,因为period_date不同
将两部分结合起来
select (round((m1.data_1-m2.data_2)/m2.data_2*100,2),'%') as increasing
from
(select period_date,max(data_value) as data_1
from `macro industry`
where year(PERIOD_DATE)=2015 and indic_id=2020101522
group by period_date
order by data_1 desc
limit 1) m1-- 2015年最高值
join
(select period_date,max(data_value) as data_2
from `macro industry`
where year(PERIOD_DATE)=2014 and indic_id=2020101522
group by period_date) m2
on year(m1.period_date)=year(m2.period_date) + 1
and month(m1.period_date)=month(m1.period_date);
练习题6.5
试统计在2016年6月期间,线上总体优惠券弃用率为多少?并找出优惠券弃用率最高的商家。
弃用率 = 被领券但未使用的优惠券张数 / 总的被领取优惠券张数
- 总体弃用率
select concat(round(count(date)/count(coupon_id),2)*100,'%') as discard_rate
from ccf_online_stage1_train
where year(date_received)=2016 and month(date_received)=6;
结果如下:
- 优惠券弃用率最高的商家
select Merchant_id,concat(round(count(date)/count(coupon_id),2)*100,'%') as discard_rate
from ccf_online_stage1_train
where year(date_received)=2016 and month(date_received)=6
group by Merchant_id
order by discard_rate desc
limit 1;
结果如下:
练习题6.6
找出 pH=3.63的所有白葡萄酒,然后,对其 residual sugar 量进行英式排名(非连续的排名)
发现表名里不是下划线也没关系,只要用``括起来就可以
select PH,`residual sugar`,rank() over(order by `residual sugar`) as ranking2
from `winequality-white`
where PH=3.63;
结果如下:
练习题6.7
请使用A股上市公司季度营收预测中的数据集《Market Data.xlsx》中的sheet-DATA,
计算截止到2018年底,市值最大的三个行业是哪些?以及这三个行业里市值最大的三个公司是哪些?(每个行业找出前三大的公司,即一共要找出9个)
- 前三的行业
select type_name_en,sum(market_value) as type_rank
from `market data`
where year(end_date)<=2018
group by TYPE_NAME_EN
order by type_rank desc
limit 3;
- 前三的前三
select m2.type_name_en,m1.ticker_symbol
from
(select ticker_symbol,type_name_en,
row_number() over (partition by type_name_en order by market_value desc) as com_rank
from `market data`
where year(end_date)<=2018) m1-- 公司排名
join
(select type_name_en,sum(market_value) as type_rank
from `market data`
where year(end_date)<=2018
group by TYPE_NAME_EN
order by type_rank desc
limit 3) m2
on m1.type_name_en=m2.type_name_en
where com_rank<=3;
- 结果为: