我试图显示从出生日期列计算的每个年龄的计数,但问题是它只会按出生日期分组,因此显示了很多重复的年龄。这是我的查询:
SELECT
DATEDIFF(hh, p.p_dob, GETDATE())/8766 AS Age,
COUNT(DISTINCT s.s_studentreference) AS 'Count'
FROM
moduleenrolment e
INNER JOIN
module m ON m.m_id = e.e_module
INNER JOIN
person p ON p.p_id = e.e_student
INNER JOIN
student s ON s.s_id = p.p_id
WHERE
m.m_reference LIKE '11%'
AND LEN(m.m_reference) <= 12
AND e.e_status IN ('A', 'W', 'C')
GROUP BY
p.p_dob
ORDER BY
p.p_dob DESC
这是结果的示例
12 1
13 1
14 1
14 1
14 1
14 1
14 1
14 1
14 1
14 1
14 1
我一直在寻找的是
12 1
13 1
14 9