0

对不起,如果这是一个新手问题。是否可以在单个查询中使用不同的 where 条件获取 Count 列表。基本上,我正在尝试获取按年龄显示申请人数量的列表。示例 20 - 29 30 - 39 等

非常感谢,安迪

4

2 回答 2

0

也许这样的事情对你有用

SELECT SUM(case where DATEDIFF(YEAR, [dob], GETDATE()) between 20 and 29 then 1 else 0) AS 'Age_20_29',
SUM(case where DATEDIFF(YEAR, [dob], GETDATE()) between 30 and 39 then 1 else 0) AS 'Age_30_39',
[etc.]
FROM myTbl
于 2012-04-05T22:27:47.973 回答
0
;WITH AgeGroups AS (
SELECT
  CASE 
    WHEN FLOOR(DATEDIFF(DAY, DOB, GETDATE()) / 365.25 BETWEEN 20 AND 29 
      THEN '20 - 29'
    WHEN FLOOR(DATEDIFF(DAY, DOB, GETDATE()) / 365.25 BETWEEN 30 AND 39 
      THEN '30 - 39'
  END AS AgeGroup
FROM YourTable
)
SELECT
  AgeGroup,
  COUNT(*) AS AgeGroupCount
FROM AgeGroups
GROUP BY AgeGroup
于 2012-04-05T22:23:33.597 回答