我發現這個在這裏解決, sql query that groups different items into bucketsMySQL查詢組數據轉換成不同的範圍
它運作良好,只要有對所有範圍的數據。如果沒有數據,我希望查詢返回0。
所以,如果我的表是:
item_name | price
i1 | 2
i2 | 22
i3 | 4
i4 | 26
i5 | 44
i6 | 6
我想輸出:
下面的查詢將不會顯示效果爲0計數的情況。
select
case when price >= 0 and price <= 10 then " 0 - 10"
when price > 10 and price <= 20 then " 10 - 20"
when price > 20 and price <= 30 then " 20 - 30"
when price > 30 and price <= 40 then " 30 - 40"
when price > 40 and price <= 50 then " 40 - 50"
else "over 50"
end PriceRange,
count(*) as TotalWithinRange
from
YourTable
group by 1
有沒有人有這方面的解決方案?
你有一個顯示問題。基本查詢很簡單:'選擇cast(價格/ 10作爲int),通過cast(price/10 as int)'從yourtable組計數(*),然後使用一些客戶端代碼將轉換值格式化爲您的範圍。 –