为了账号安全,请及时绑定邮箱和手机立即绑定

SQL-'聚合查询'-按10元为一个档次,查询单价各档次的数量

SQL-'聚合查询'-按10元为一个档次,查询单价各档次的数量

阿晨1998 2018-12-07 01:06:20
select sum(a.a) '0-10',sum(a.b) '10-20',sum(a.c) '20-30',sum(a.d) '30以上'from (select case when price>0 and price<=10 then 1 end as a,case when price>10 and price<=20 then 1 end as b,case when price>20 and price<=30 then 1 end as c,case when price>30 then 1 end as dfrom titles) as a   这是我用  CASE when  条件判断。老师说虽然说结果对,但是这个写着太复杂了,当遇到1000条。 是那我就要写几百 几千条CASE when 加以判断, 所以小弟在这求根据问题标题的要求写出结果一样的,比较简洁的语句,注意是聚合查询。
查看完整描述

12 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

你好· 谢谢你大哥·~! 这个语句不能在用 这个 条件判断写了·~!
查看完整回答
反对 回复 2019-01-07
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

你好;这个问题 穿越自己的知识层面了·~再次谢谢你的关注·~!
查看完整回答
反对 回复 2019-01-07
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

暂时还没有想到很好的解决方案. 我写的这个也不是太好,比较的复杂 select [a] as '0-10',[b] as '10-20',[c] as '20-30',[d] as '>30' from( select 'a' as flag,sum(price) as sumprice from title where price>0 and price<=10 union all select 'b' as b,sum(price) from title where price>10 and price<=20 union all select 'c' as b,sum(price) from title where price>20 and price<=30 union all select 'd' as b,sum(price) from title where price>30 ) PRICE pivot ( sum(sumprice) for flag in ([a],[b],[c],[d]) ) as PVT 还需要在寻求比较优化的方法,谢谢!
查看完整回答
反对 回复 2019-01-07
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

谢谢
查看完整回答
反对 回复 2019-01-07
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

运行环境:MSSQL2005
查看完整回答
反对 回复 2019-01-07
?
心有法竹

TA贡献1866条经验 获得超5个赞

你好大哥!谢谢你的关注,你的结果是对的,但是你这样的效果与case when 语句差不多,
查看完整回答
反对 回复 2019-01-07
  • 12 回答
  • 0 关注
  • 548 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信