SQL一些常用操作

1. 分组统计

根据 h3_index进行分组统计,计算分组内order_id的数量,并将列名重命名为cha_cnt;计算分组内total_power的总和,并将列名重命名为cha_power;对分组内的vid进行去重,并统计去重后分组内vid的数量,并将列名重命名为ev_cnt

select h3_index, count(order_id) as cha_cnt, sum(total_power) as cha_power, count(distinct vid) as ev_cnt
from data_charge
group by h3_index;

将查询结果保存到已经创建好的数据表data_charge_1中。

insert into data_charge_1
select h3_index, count(order_id) as cha_cnt, sum(total_power) as cha_power, count(distinct vid) as ev_cnt
from data_charge
group by h3_index;



行转列

根据h3_index进行分组统计,将列cat1中的数据作为列名(就是下面这些文字部分),然后统计每个分组内这些特征的数量。

select 
	h3_index,
	sum(case when cat1='汽车服务' then 1 else 0 end) '汽车服务',
	sum(case when cat1='购物服务' then 1 else 0 end) '购物服务',
	sum(case when cat1='商务住宅' then 1 else 0 end) '商务住宅',
	sum(case when cat1='生活服务' then 1 else 0 end) '生活服务',
	sum(case when cat1='医疗保健服务' then 1 else 0 end) '医疗保健服务',
	sum(case when cat1='风景名胜' then 1 else 0 end) '风景名胜',
	sum(case when cat1='政府机构及社会团体' then 1 else 0 end) '政府机构及社会团体',
	sum(case when cat1='交通设施服务' then 1 else 0 end) '交通设施服务',
	sum(case when cat1='地名地址信息' then 1 else 0 end) '地名地址信息',
	sum(case when cat1='道路附属设施' then 1 else 0 end) '道路附属设施',
	sum(case when cat1='科教文化服务' then 1 else 0 end) '科教文化服务',
	sum(case when cat1='体育休闲服务' then 1 else 0 end) '体育休闲服务',
	sum(case when cat1='公司企业' then 1 else 0 end) '公司企业',
	sum(case when cat1='金融保险服务' then 1 else 0 end) '金融保险服务',
	sum(case when cat1='餐饮服务' then 1 else 0 end) '餐饮服务',
	sum(case when cat1='住宿服务' then 1 else 0 end) '住宿服务'
from data_poi
group by h3_index

类似地,将查询结果保存到已经创建好的数据表data_poi_1中。

insert into algorithm_data.data_poi_1
select 
	h3_index,
	sum(case when cat1='汽车服务' then 1 else 0 end) '汽车服务',
	sum(case when cat1='购物服务' then 1 else 0 end) '购物服务',
	sum(case when cat1='商务住宅' then 1 else 0 end) '商务住宅',
	sum(case when cat1='生活服务' then 1 else 0 end) '生活服务',
	sum(case when cat1='医疗保健服务' then 1 else 0 end) '医疗保健服务',
	sum(case when cat1='风景名胜' then 1 else 0 end) '风景名胜',
	sum(case when cat1='政府机构及社会团体' then 1 else 0 end) '政府机构及社会团体',
	sum(case when cat1='交通设施服务' then 1 else 0 end) '交通设施服务',
	sum(case when cat1='地名地址信息' then 1 else 0 end) '地名地址信息',
	sum(case when cat1='道路附属设施' then 1 else 0 end) '道路附属设施',
	sum(case when cat1='科教文化服务' then 1 else 0 end) '科教文化服务',
	sum(case when cat1='体育休闲服务' then 1 else 0 end) '体育休闲服务',
	sum(case when cat1='公司企业' then 1 else 0 end) '公司企业',
	sum(case when cat1='金融保险服务' then 1 else 0 end) '金融保险服务',
	sum(case when cat1='餐饮服务' then 1 else 0 end) '餐饮服务',
	sum(case when cat1='住宿服务' then 1 else 0 end) '住宿服务'
from data_poi
group by h3_index
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值