亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 598 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號