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
返回:
研究不易,转载请注明出处