1)表名:macro_index_data
2)字段名:数据期(年月)(occur_period)、地区代码(area_code)、指标代码(index_code)、指标类型(增速、总量)(index_type)、指标值(index_value)、数据更新时间(update_time)。说明:罗湖区的区划代码为440305000000、GDP指标代码为gmjj_jjzl_01、指标类型的枚举值分别是增速(TB)、总量(JDZ)
3)请写出,2020年4个季度中GDP的增速都超过罗湖区同期的区有哪些
SQL如下:
--最后比较指标,若都大于即为所求的区
select t2.area_code
from (
--先求出罗湖区2020年四季度的GDP指标
select area_code,
sum(case when month('occur_period') between 1 and 3 then index_value else 0 end) `one`, --一季度GDP指标
sum(case when month('occur_period') between 4 and 6 then index_value else 0 end) `two`, --二季度GDP指标
sum(case when month('occur_period') between 7 and 9 then index_value else 0 end) `three`, --三季度GDP指标
sum(case when month('occur_period') between 10 and 12 then index_value else 0 end) `four` --四季度GDP指标
from macro_index_data
where area_code = '440305000000' --罗湖区
and index_code = 'gmjj_jjzl_01' --GDP指标
and index_type = 'TB' --增速
and year('occur_period') = 2020 --2020年
group by area_code
) t1
join (
--再求出其它区2020年四季度的GDP指标
select area_code,
sum(case when month('occur_period') between 1 and 3 then index_value else 0 end) `one`, --一季度GDP指标
sum(case when month('occur_period') between 4 and 6 then index_value else 0 end) `two`, --二季度GDP指标
sum(case when month('occur_period') between 7 and 9 then index_value else 0 end) `three`, --三季度GDP指标
sum(case when month('occur_period') between 10 and 12 then index_value else 0 end) `four` --四季度GDP指标
from macro_index_data
where area_code <> '440305000000' --其它区
and index_code = 'gmjj_jjzl_01' --GDP指标
and index_type = 'TB' --增速
and year('occur_period') = 2020 --2020年
group by area_code
) t2 on t2.one > t1.one
and t2.two > t1.two
and t2.three > t1.three
and t2.four > t1.four;