TASK6
练习题1
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用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
解答:
要求以一定条件合并表的信息,用 inner join + on 语句即可。
SELECT M.TICKER_SYMBOL, M.END_DATE, M.CLOSE_PRICE,
I.TICKER_SYMBOL, I.END_DATE, I.T_REVENUE, I.T_COGS, I.N_INCOME,
C.TICKER_SYMBOL, C.INDIC_NAME_EN, C.END_DATE, C.VALUE
FROM `market data` AS M
INNER JOIN `Income Statement` AS I ON M.TICKER_SYMBOL = I.TICKER_SYMBOL
INNER JOIN `Company Operating` AS C ON I.TICKER_SYMBOL = C.TICKER_SYMBOL
WHERE M.TICKER_SYMBOL = 600383 OR M.TICKER_SYMBOL = 600048;
运行结果:
练习题2
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=44
请使用 Wine Quality Data 数据集《winequality-red.csv》,找出 pH=3.03的所有红葡萄酒,然后,对其 citric acid 进行中式排名(相同排名的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”)
解答:
要求中式排名,用 窗口函数 + DENSE_RANK() 语句即可。
SELECT DENSE_RANK() OVER(ORDER BY W.`citric acid`) AS 'RANK', W.*
FROM `winequality-white` AS W
WHERE W.pH = 3.03;
运行结果:
练习题3
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_offline_stage1_test_revised.csv》,试分别找出在2016年7月期间,发放优惠券总金额最多和发放优惠券张数最多的商家。
这里只考虑满减的金额,不考虑打几折的优惠券。
解答:
求发放优惠券张数最多的商家:
由于 User_id 列是唯一的,即每个人只领导了一张优惠券,则按 User_id 列计数并按 Merchant_id(商家ID) 列分组即可
select `Merchant_id` as '发放优惠券张数最多的商家', count( `User_id`) as '发放张数'
from `ccf_offline_stage1_test_revised`
where year (`Date_received`) = 2016
and month(`Date_received`) = 07
group by `Merchant_id`
order by `发放张数` desc limit 1;
运行结果:
求发放优惠券总金额最多的商家:
优惠券金额的信息包含在 Discount_rate 列,但由于 Discount_rate 列中数据的形式为 x:y,表示满 x 减 y,因此需要将优惠券金额通过字符串操作函数找出来,再进行求和即可
select `Merchant_id` as '发放优惠券总金额最多的商家',
sum(right(Discount_rate,length(`Discount_rate`)-locate(':',`Discount_rate`))) as '优惠券总金额'
from `ccf_offline_stage1_test_revised`
group by `发放优惠券总金额最多的商家`
order by `优惠券总金额` desc limit 1;
运行结果:
练习题4
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用A股上市公司季度营收预测中的数据集《Macro&Industry.xlsx》中的sheet-INDIC_DATA,请计算全社会用电量:第一产业:当月值在2015年用电最高峰是发生在哪月?并且相比去年同期增长/减少了多少个百分比?
解答:
求 " 全社会用电量:第一产业: 当月值 " 在2015年用电最高峰是发生在哪月:
SELECT SUM(DATA_VALUE) AS 'SUM', MONTH(PERIOD_DATE) AS 'MONTH' FROM `macro industry`
WHERE YEAR(PERIOD_DATE) = 2015
GROUP BY `MONTH`
ORDER BY `SUM` LIMIT 1;
运行结果:
求相比去年同期增长/减少了多少个百分比:
SELECT SUM(DATA_VALUE) AS 'SUM',
MONTH(PERIOD_DATE) AS 'MONTH',
`SUM_L`,
`MONTH_L`,
(SUM(DATA_VALUE)/`SUM_L`-1)*100 AS '增长百分比'
FROM `macro industry` AS T
INNER JOIN (
SELECT SUM(DATA_VALUE) AS 'SUM_L', MONTH(PERIOD_DATE) AS 'MONTH_L'
FROM `macro industry`
WHERE YEAR(PERIOD_DATE) = 2014
GROUP BY `MONTH_L`
) AS L
ON L.`MONTH_L` = MONTH(PERIOD_DATE)
WHERE YEAR(PERIOD_DATE) = 2015
GROUP BY `MONTH`,`MONTH_L`
ORDER BY `SUM` LIMIT 1;
运行结果:
练习题5
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_online_stage1_train.csv》,试统计在2016年6月期间,线上总体优惠券弃用率为多少?并找出优惠券弃用率最高的商家。
弃用率 = 被领券但未使用的优惠券张数 / 总的被领取优惠券张数
解答:
通过条件筛选得出结论:如果一张优惠券被使用,则不会出现它被领取的记录,即同一张优惠券(相同 Coupon_id 并不代表同一张优惠券)的不会出现两次记录。
因此要获得被领取的优惠券总数,直接 count 即可得到,使用数可以在总数的基础上取 Date 不为空值即可,以上条件均需排除打折活动的记录(即 Coupon_id 是 “fixed” 的记录)
求线上总体优惠券弃用率:
SELECT COUNT(*) AS '总的被领取优惠券张数',
(SELECT COUNT(*)
FROM `ccf_online_stage1_train`
WHERE `Coupon_id` IS NOT NULL AND `Coupon_id` <> 'fixed'
AND `Date` IS NULL
AND YEAR(`Date_received`) = 2016 AND MONTH( `Date_received`) = 06
)AS '被领券但未使用的优惠券张数'
FROM `ccf_online_stage1_train`
WHERE `Coupon_id` IS NOT NULL AND `Coupon_id` <> 'fixed'
AND YEAR(`Date_received`) = 2016 AND MONTH( `Date_received`) = 06
运行结果:
求优惠券弃用率最高的商家:
SELECT `Merchant_id`,
`总的被领取优惠券张数`,
COUNT(*) AS '被领券但未使用的优惠券张数',
100*COUNT(*)/`总的被领取优惠券张数` AS '优惠券弃用率(%)'
FROM `ccf_online_stage1_train`
INNER JOIN (
SELECT `Merchant_id` AS 'M_G', COUNT(*) AS '总的被领取优惠券张数'
FROM `ccf_online_stage1_train`
WHERE `Coupon_id` IS NOT NULL AND `Coupon_id` <> 'fixed'
AND YEAR(`Date_received`) = 2016 AND MONTH( `Date_received`) = 06
GROUP BY `Merchant_id`
) AS G
ON `Merchant_id` = G.`M_G`
WHERE `Coupon_id` IS NOT NULL AND `Date` IS NULL AND `Coupon_id` <> 'fixed'
AND YEAR(`Date_received`) = 2016 AND MONTH( `Date_received`) = 06
GROUP BY `Merchant_id`
ORDER BY `优惠券弃用率(%)` DESC ;
运行结果:
练习题6
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=44
请使用 Wine Quality Data 数据集《winequality-white.csv》,找出 pH=3.63的所有白葡萄酒,然后,对其 residual sugar 量进行英式排名(非连续的排名)
解答:
和练习题2类似,要求英式排名,用 窗口函数 + RANK() 语句即可:
SELECT RANK() OVER(ORDER BY W.`residual sugar`) AS 'RANK', ph, W.*
FROM `winequality-red` AS W
WHERE W.pH = 3.63;
运行结果:
练习题7
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用A股上市公司季度营收预测中的数据集《Market Data.xlsx》中的sheet-DATA,
计算截止到2018年底,市值最大的三个行业是哪些?以及这三个行业里市值最大的三个公司是哪些?(每个行业找出前三大的公司,即一共要找出9个)
解答:
根据题意计算时间节点为“截止2018年底”,通过筛选后发现该表数据结构为 “ 每个公司每个时间点 ” 为一条,且 END_DATE 最晚为 2018-05-31 ,因此直接统计时间点为 2018-05-31 的数据即可。
首先第一步需找出各行业市值前三的公司,用排名 窗口函数+PARTITION BY 行业+ORDER BY 市值 倒序 即可。
其次计算出各行业的总市值,用 INNER JOIN 连结两次查询,最后筛选出前三名即可:
SELECT SUM(A.`MARKET_VALUE`) AS '行业总市值',B.* FROM `market data` AS A
INNER JOIN (SELECT TYPE_NAME_CN ,TICKER_SYMBOL,`MARKET_VALUE`,
RANK()
OVER(PARTITION BY TYPE_NAME_CN
ORDER BY `MARKET_VALUE`+0 DESC) AS 'RANK'
FROM `market data`
WHERE END_DATE = '2018-05-31'
)AS B
ON A.TYPE_NAME_CN = B.TYPE_NAME_CN
WHERE END_DATE = '2018-05-31'
GROUP BY B.TYPE_NAME_CN, B.TICKER_SYMBOL, B.`MARKET_VALUE`
HAVING `RANK`<=3
ORDER BY `行业总市值` DESC, `RANK` LIMIT 9
运行结果:
练习题8
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_online_stage1_train.csv》和《ccf_offline_stage1_train.csv》,试找出在2016年6月期间,线上线下累计优惠券使用次数最多的顾客。
解答:
根据题意将两张表符合条件的顾客筛选出来,使用 union 合并再 count 即可:
SELECT A.`User_id`, COUNT(*) AS '优惠券使用次数'
FROM (
(SELECT OFF.User_id, OFF.Date_received, OFF.Date FROM `ccf_offline_stage1_train` AS OFF
WHERE `Coupon_id` IS NOT NULL AND `Date` IS NOT NULL AND LEFT(`Date_received`,6)='201606')
UNION
(SELECT `ON`.User_id, `ON`.Date_received, `ON`.Date FROM `ccf_online_stage1_train` AS `ON`
WHERE `Coupon_id` <> 'fixed' AND `Coupon_id` IS NOT NULL AND `Date` IS NOT NULL AND YEAR(`Date_received`) = 2016 AND MONTH(`Date_received`) = 6)
) AS A
GROUP BY A.`User_id`
ORDER BY COUNT(*) DESC
-- LIMIT 1
运行结果:
练习题9
数据来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=1074
请使用A股上市公司季度营收预测数据集《Income Statement.xls》中的sheet-General Business和《Company Operating.xlsx》中的sheet-EN。
找出在数据集所有年份中,按季度统计,白云机场旅客吞吐量最高的那一季度对应的净利润是多少?(注意,是单季度对应的净利润,非累计净利润。)
解答:
根据题意首先找出最大旅客吞吐量季度——利用 sum 窗口函数分年分季度进行统计即可:
SELECT INDIC_NAME_EN,
SUM(`VALUE`) OVER( PARTITION BY `YEAR`, SEASON) AS S,
`YEAR`,
SEASON
FROM
(SELECT INDIC_NAME_EN, `VALUE`,END_DATE, YEAR(END_DATE) AS 'YEAR',
CASE WHEN MONTH(END_DATE) IN (1,2,3) THEN 1
WHEN MONTH(END_DATE) IN (4,5,6) THEN 2
WHEN MONTH(END_DATE) IN (7,8,9) THEN 3
WHEN MONTH(END_DATE) IN (10,11,12) THEN 4
END AS SEASON
FROM `company operating`
WHERE INDIC_NAME_EN = 'Baiyun Airport:Passenger throughput') AS P
ORDER BY S DESC
运行结果:
可以看出旅客吞吐量最高的是 2018 年的第一季度 ,进而查询字段表得知, T_PROFIT 为总利润, REPORT_TYPE 为 Q1 的为季度报告, END_DATE 为截止日期,因此只要查询对应季度的总利润即可:
SELECT `T_PROFIT`, A.* FROM `income statement` AS A
WHERE TICKER_SYMBOL = 600004 AND REPORT_TYPE = 'Q1' AND END_DATE = '2018-03-31';
运行结果:
(该季度利润为 596,388,972 元 )
练习题10
数据来源:https://tianchi.aliyun.com/competition/entrance/231593/information
使用Coupon Usage Data for O2O中的数据集《ccf_online_stage1_train.csv》和《ccf_offline_stage1_train.csv》,试找出在2016年6月期间,线上线下累计被使用优惠券满减最多的前3名商家。
比如商家A,消费者A在其中使用了一张200减50的,消费者B使用了一张30减1的,那么商家A累计被使用优惠券满减51元。
解答:
和练习3类似,根据题意先合并两表中所有商家被使用优惠券的行,在进行累加即可:
SELECT `Merchant_id` , SUM(A) AS '累计被使用优惠券满减的金额'
FROM (
SELECT Merchant_id,
RIGHT(Discount_rate,LENGTH( `Discount_rate`) - LOCATE(':',`Discount_rate` ) )AS A
FROM `ccf_online_stage1_train`
WHERE `Date_received` IS NOT NULL AND `Date` IS NOT NULL AND `Coupon_id` <> 'fixed'
UNION
SELECT Merchant_id,
RIGHT(Discount_rate,LENGTH( `Discount_rate`) - LOCATE(':',`Discount_rate` ) )AS A
FROM `ccf_offline_stage1_train`
WHERE `Date_received` IS NOT NULL AND `Date` IS NOT NULL) AA
GROUP BY `Merchant_id`
ORDER BY SUM(A) DESC
-- LIMIT 1
运行结果:
如图被使用优惠券金额最多的 商家ID为 54303。