面试题:使用SQL选出下表中6个指标至少4个大于50的城市

题目:
青岛,96,50,56,55,43,21
北京,74,16,96,29,54,4
南京,5,52,18,82,18,83
答案:
1.建表

drop table if exists t_index;
create table t_index(
	city string,
	od int,
	sc int,
	pc int,
	hc int,
	gc int,
	ghc int
) row format delimited fields terminated by ',';
青岛,96,50,56,55,43,21
北京,74,16,96,29,54,4
南京,5,52,18,82,18,83

2.加载数据
load data locla inpath ‘/root/data/t_index.txt’ into table t_index;
3.SQL查询

select 
tmp.city,
sum(tmp.t1 + tmp.t2 + tmp.t3 + tmp.t4 + tmp.t5 +tmp.t6) as s
from
(
select 
	city,
	if(od>=50,1,0) as t1,
	if(sc>=50,1,0) as t2,
	if(pc>=50,1,0) as t3,
	if(hc>=50,1,0) as t4,
	if(gc>=50,1,0) as t5,
	if(ghc>=50,1,0) as t6
from 
	t_index 
) tmp
group by 
	tmp.city
having s>=4;

4.结果展示
tmp.city s
青岛 4


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值