sql之分段統計
阿新 • • 發佈:2018-09-30
rom sel src 技術分享 group mage inf size dbo
sql之分段統計
需求:獲取一個縣所有家庭人數在1-2人,3-4人,5-6人,6人以上的家庭數的數組
思路:通過CASE WHEN 將 CBFCYSL分組,然後統計數據條數。
語句:
SELECT CASE WHEN CBFCYSL<=2 THEN ‘1-2‘ WHEN CBFCYSL<=4 AND CBFCYSL>2 THEN ‘2-3‘ WHEN CBFCYSL<=6 AND CBFCYSL>4 THEN ‘4-5‘ WHEN CBFCYSL>6 THEN‘6+‘ ELSE ‘其他‘ END AS QJ, COUNT(CBFCYSL) AS NUM FROM TDCBJYQ.dbo.CBF GROUP BY CASE WHEN CBFCYSL<=2 THEN ‘1-2‘ WHEN CBFCYSL<=4 AND CBFCYSL>2 THEN ‘2-3‘ WHEN CBFCYSL<=6 AND CBFCYSL>4 THEN ‘4-5‘ WHEN CBFCYSL>6 THEN ‘6+‘ ELSE‘其他‘ END
返回:
研究不易,轉載請註明出處
sql之分段統計