两个日期范围之间的SQL Count年龄

时间:2014-09-29 08:08:45

标签: sql-server datetime count range

我正在尝试将数据库中活动记录的年龄从当前日期计算到可能的过去日期。 这是可能的输出:

15至24岁 12

年龄24至30岁 15

30岁及以上
25

并计算年龄

age     count
15        3
16        5
17        1
18        2
19        1
20        0
....     ....

2 个答案:

答案 0 :(得分:2)

要计算年龄:

;With Ages
AS
(
    SELECT CASE 
            WHEN MONTH(getdate()) > MONTH([YourDate]) THEN DATEDIFF(YEAR, [YourDate], getdate())
            WHEN Month([YourDate]) = MONTH(Getdate()) AND Day([YourDate]) < Day(getdate())
            THEN DATEDIFF(YEAR, [YourDate], getdate()) 
            ELSE DATEDIFF(YEAR, [YourDate], getdate())  - 1 END As Age
    FROM #YourTable
)
SELECT Age, count(*) AS [Count]
FROM Ages
GROUP BY Age

要获得日期范围计数:

;With Ages
AS
(
    SELECT CASE 
            WHEN MONTH(getdate()) > MONTH([YourDate]) THEN DATEDIFF(YEAR, [YourDate], getdate())
            WHEN Month([YourDate]) = MONTH(Getdate()) AND Day([YourDate]) < Day(getdate())
            THEN DATEDIFF(YEAR, [YourDate], getdate()) 
            ELSE DATEDIFF(YEAR, [YourDate], getdate())  - 1 END As Age
    FROM #YourTable
),
AgeCount
AS
(
    SELECT Age, count(*) AS [Count]
    FROM Ages
    GROUP BY Age
)
SELECT  SUM(CASE WHEN Age >= 15 and Age < 24 THEN 1 ELSE 0 END) As [15-24],
        SUM(CASE WHEN AGE >= 25 AND Age < 30 Then 1 ELSE 0 END) As [25-30],
        SUM(CASE WHEN AGE > 30 THEN 1 ELSE 0 END) AS [Over 30]
From AgeCount

答案 1 :(得分:0)

试试这个

DECLARE @DATETBL TABLE
(
 ID INT IDENTITY(1,1),
 POSSIBLEDATES DATETIME
)
DECLARE @AGETBL TABLE
(
ID INT IDENTITY(1,1),
AGE INT
)
DECLARE @I INT=1
DECLARE @CNT INT

INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1970')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1972')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1975')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1977')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1977')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1977')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1977')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1980')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1983')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1983')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1983')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1986')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1986')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1986')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1986')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1986')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1986')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1989')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1989')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1989')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1989')
INSERT INTO @DATETBL(POSSIBLEDATES) VALUES ('01/01/1990')

SELECT @CNT=COUNT(*) FROM @DATETBL

WHILE @I<=@CNT
BEGIN

DECLARE @BIRTHDATE DATETIME = (SELECT POSSIBLEDATES FROM @DATETBL WHERE ID=@I)

INSERT INTO @AGETBL(AGE)
SELECT YEAR(GETDATE()) - YEAR(@BIRTHDATE) -
       CASE WHEN MONTH(GETDATE()) > MONTH(@BIRTHDATE) OR
                (MONTH(GETDATE()) = MONTH(@BIRTHDATE) AND
                 DAY(GETDATE())  >= DAY(@BIRTHDATE))
            THEN 0 ELSE 1 END AS [AGE]
 SET @I=@I+1           
END    

SELECT AGE,COUNT(*) AGECOUNT FROM @AGETBL WHERE AGE BETWEEN 15 AND 24
OR AGE BETWEEN 25 AND 30  
OR AGE >30      
GROUP BY AGE
ORDER BY AGE

获得的结果

Age AgeCount
25  4
28  6
31  3
34  1
37  4
39  1
42  1
44  1

Demo

对于Above,我曾尝试使用Temp Tables,你可以在数据库中使用你自己的表