我試圖創建此概要統計數據表,從該表中年齡間隔與SQL
PersonID | Age | Sex
----------------------------
A | 43 | F
B | 22 | F
C | 65 | M
D | 33 | F
E | 28 | M
計數在每個類別
Agegroup | All | Female | Male
------------------------------------------------
All | 560594 | 34324 | 234244
< 20 | 4324 | 545 | 3456
20 - 30 | 76766 | 3424 | 32428
30 - 40 | 36766 | 764 | 82427
40 - 50 | 46766 | 4324 | 72422
50 - 60 | 66766 | 3424 | 52424
> 60 | 76766 | 43424 | 12423
的個體數列的統計資料這甚至可能「一氣呵成」與SQL?我嘗試了這一點,但它不是真正的走到一起..
SELECT SUM(CASE WHEN Age < 20 THEN 1 ELSE 0 END) AS [Under 20],
SUM(CASE WHEN Age BETWEEN 20 AND 30 THEN 1 ELSE 0 END) AS [20-30],
SUM(CASE WHEN Age BETWEEN 30 AND 40 THEN 1 ELSE 0 END) AS [30-40]
FROM Persons
你在使用MSSQL,ORACEL,MySQL的? – Arion
對,sql服務器 – jenswirf
@wije - 在什麼樣的年齡組你會期望30歲的人? '20-30','30-40'還是兩者兼而有之? –