2019.1.7mysql总结(优化代码)

跨库查询修改添加

 

Top10.社区人口数量柱状图

USE test;

SELECT AREANAME,COUNT(AREANAME)AS NUMB FROM data_aggregation.ZFW_WGH_V_LDPEO GROUP BY AREANAME LIMIT 10

 

创建相应社区表

CREATE TABLE Community_population SELECT AREANAME,COUNT(AREANAME)AS NUMB FROM data_aggregation.ZFW_WGH_V_LDPEO GROUP BY AREANAME LIMIT 10

 

 

 

#########社区人口

USE test;

CREATE TABLE Community_population SELECT AREANAME,COUNT(AREANAME)AS NUMB FROM data_aggregation.ZFW_WGH_V_LDPEO GROUP BY AREANAME LIMIT 10

#########省份人口占比

CREATE TABLE Province_10

select case

when province_no='11' then '北京市'

when province_no='12' then '天津市'

when province_no='13' then '河北省'

when province_no='14' then '山西省'

when province_no='15' then '内蒙古自治区'

when province_no='21' then '辽宁省'

when province_no='22' then '吉林省'

when province_no='23' then '黑龙江省'

when province_no='31' then '上海市'

when province_no='32' then '江苏省'

when province_no='33' then '浙江省'

when province_no='34' then '安徽省'

when province_no='35' then '福建省'

when province_no='36' then '江西省'

when province_no='37' then '山东省'

when province_no='41' then '河南省'

when province_no='42' then '湖北省'

when province_no='43' then '湖南省'

when province_no='44' then '广东省'

when province_no='45' then '广西壮族自治区'

when province_no='46' then '海南省'

when province_no='50' then '重庆市'

when province_no='51' then '四川省'

when province_no='52' then '贵州省'

when province_no='53' then '云南省'

when province_no='54' then '西藏自治区'

when province_no='61' then '陕西省'

when province_no='62' then '甘肃省'

when province_no='63' then '青海省'

when province_no='64' then '宁夏回族自治区'

when province_no='65' then '新疆维吾尔自治区'

when province_no='71' then '台湾省'

when province_no='81' then '香港特别行政区'

when province_no='82' then '澳门特别行政区'

else '未知'    

end as Province,total as counts

from (select left(IDENTIFICATION_NUMBER,2) as province_no,count(*) as total from data_aggregation.ZFW_WGH_V_LDPEO

group by province_no ) bak ORDER BY counts DESC LIMIT 10

#######Age_interval_20

CREATE table tmp_0105 SELECT * FROM(

SELECT ID,name,(YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) AS ages

FROM data_aggregation.ZFW_WGH_V_LDPEO HAVING ages <100 ) A;

 

CREATE table Age_ratio SELECT * FROM(

SELECT ages,(CASE

       WHEN ages < 20 THEN '0-19'

       when ages < 40 THEN '20-39'

       WHEN ages < 60 THEN '40-59'

       when ages < 80 then '60-79'

       ELSE '89+'

       END)B from tmp_0105

ORDER BY ages ) A;

 

CREATE TABLE Age_ratio_20 SELECT * FROM(

select B,COUNT(B) C  FROM Age_ratio

GROUP BY B )D;

DROP TABLE tmp_0105,Age_ratio;

 

优化一下,在一个表写

select * from

(

select B,count(B) from

  (select

  ID,

  name,

  (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) as ages,

  case

    when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) <20 then '0-19'

    when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) <40 then '20-39'

    when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) < 60 THEN '40-59'

    when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) < 80 then '60-79'

    ELSE '89+'

    END as B

  from data_aggregation.ZFW_WGH_V_LDPEO HAVING ages <100) tmp

  group by B ORDER BY COUNT(B)DESC

) target

 

 

 

 

按照需求优化代码

#####top10社区人口
SELECT AREANAME,COUNT(AREANAME)AS NUMB FROM data_aggregation.ZFW_WGH_V_LDPEO GROUP BY AREANAME LIMIT 10

####年龄分段统计
select * from
(
select B,count(B) from 
	(select 
	ID,
	name,
	(YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) as ages,
	case 
		when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) <20 then '0-19'
		when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) <40 then '20-39'
		when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) < 60 THEN '40-59'
		when (YEAR(CURDATE()) - SUBSTRING(`IDENTIFICATION_NUMBER`,7,4)) < 80 then '60-79'
		ELSE '89+'
		END as B
	from data_aggregation.ZFW_WGH_V_LDPEO HAVING ages <100) tmp
	group by B ORDER BY COUNT(B)DESC
) target

#####各省人数占比情况
SELECT Province,
counts/(SELECT count(*)FROM data_aggregation.ZFW_WGH_V_LDPEO) AS bi FROM (
select case
when province_no='11' then '北京市'
when province_no='12' then '天津市'
when province_no='13' then '河北省'
when province_no='14' then '山西省'
when province_no='15' then '内蒙古自治区'
when province_no='21' then '辽宁省'
when province_no='22' then '吉林省'
when province_no='23' then '黑龙江省'
when province_no='31' then '上海市'
when province_no='32' then '江苏省'
when province_no='33' then '浙江省'
when province_no='34' then '安徽省'
when province_no='35' then '福建省'
when province_no='36' then '江西省'
when province_no='37' then '山东省'
when province_no='41' then '河南省'
when province_no='42' then '湖北省'
when province_no='43' then '湖南省'
when province_no='44' then '广东省'
when province_no='45' then '广西壮族自治区'
when province_no='46' then '海南省'
when province_no='50' then '重庆市'
when province_no='51' then '四川省'
when province_no='52' then '贵州省'
when province_no='53' then '云南省'
when province_no='54' then '西藏自治区'
when province_no='61' then '陕西省'
when province_no='62' then '甘肃省'
when province_no='63' then '青海省'
when province_no='64' then '宁夏回族自治区'
when province_no='65' then '新疆维吾尔自治区'
when province_no='71' then '台湾省'
when province_no='81' then '香港特别行政区'
when province_no='82' then '澳门特别行政区'
else '未知'     
end as Province,
total as counts 
from (
select left(IDENTIFICATION_NUMBER,2) as province_no,count(*) as total from data_aggregation.ZFW_WGH_V_LDPEO
group by province_no ) bak  ORDER BY counts DESC )C

 

你可能感兴趣的:(大数据的点点滴滴,work_rev)