MySQL面试题---酒店信息表

在这里插入图片描述

1.统计各个星级酒店数

select datatime, 
 sum(case when xingji = '2星级' then 1 else 0 end) 2,
 sum(case when xingji = '3星级' then 1 else 0 end) 3,
 sum(case when xingji = '4星级' then 1 else 0 end) 4,
 sum(case when xingji = '5星级' then 1 else 0 end) 5from information GROUP BY datatime

2.给6家酒店打标签,把订单数量排名前50%的酒店记为A类酒店,其余记为B类酒店,输出字段为“酒店名称、星级、标签”

-- mysql 8.0版本支持开窗函数
select t1.`name`, t1.xingji, t1.orders, (case when t1.ranks<=c1.c*0.5 then 'A' else 'B' end) 标签
 from (select *,  row_number() over(ORDER BY orders desc) ranks from information ) t1, 
 (select count(1) c from information) c1

猜你喜欢

转载自blog.csdn.net/qq_42363032/article/details/108615136