计算在SQL Server中从当前日期开始的几个月内转换特定年龄的个人

时间:2017-12-06 01:50:07

标签: sql-server

我正在尝试编写一个查询,我可以根据他们的出生日期计算在使用当前日期(GETDATE)时将会年满65岁的个人。但是,该查询还要求我能够在当前日期(GETDATE)的3个月内拉出65岁的人。到目前为止,我只能通过在我的世纪领域(eebdcy)输入19的参数并在我的年度领域(eebdyr)输入52来拉动个体。我想删除这两个字段,并允许用户只需运行查询,而不必弄乱我的日期字段。这可能吗?

提前谢谢。

我的代码:

SELECT                                                              
distinct eessn AS SSN, eenaml AS LastName, eenamf AS FirstName, eenamm AS MiddleInitial,                               
         ( RIGHT('00'+ CONVERT(varchar, EEBDCY), 2) + RIGHT('00'+ CONVERT(varchar, EEBDYR), 2) + '-' + RIGHT('00'+ CONVERT(varchar, EEBDMT), 2) + '-' + RIGHT('00' + CONVERT(varchar, EEBDDY), 2) ) AS [DOB],      
         phprid AS ProductID, phacct AS AccountNumber, amplac AS  AccountName, amua12 AS MSPInd, phgrp AS GroupID,                       
         ( RIGHT('00'+ CONVERT(varchar, amfxc2), 2) + RIGHT('00'+ CONVERT(varchar, amfxy2), 2) + '-' + RIGHT('00'+ CONVERT(varchar, amfxm2), 2) + '-' + RIGHT('00' + CONVERT(varchar, amfxd2), 2) ) as MSPEFFDT,   
         substring(amuaky,1,3) as REP,
         CONVERT (date, GETDATE()) AS [TODAY],
        CAST(DATEDIFF(DD,( RIGHT('00'+ CONVERT(varchar, EEBDCY), 2) + RIGHT('00'+ CONVERT(varchar, EEBDYR), 2) + '-' + RIGHT('00'+ CONVERT(varchar, EEBDMT), 2) + '-' + RIGHT('00' + CONVERT(varchar, EEBDDY), 2) ), GETDATE())/365.25 AS INT) AS AGE   
    FROM pdthist, empmstr, actmstr         
   WHERE phgrp in ('4664 ESP','4664 XM')                              
     and phpcat = 3 and phtdcy = 0                                    
     and eessn = phssn and eegrp = phgrp                              
     and phacct = amacct and phgrp = amgrp and amtdcy = 0   
     and eebdcy = 19 and eebdyr = 52                                      
    ORDER BY phacct, eessn 

1 个答案:

答案 0 :(得分:0)

我相信这会奏效:

select
*
from your_table
cross apply (
    select DATEFROMPARTS((EEBDCY + EEBDYR), EEBDMT, EEBDDY), dateadd(month,3,getdate())
    ) ca (Birthdate, CompareDate)
where 65 <= YEAR(CompareDate) - (EEBDCY + EEBDYR)
            - ( CASE
                   WHEN MONTH(EEBDMT) > MONTH(CompareDate)
                     OR ( MONTH(EEBDMT) = MONTH(CompareDate)
                        AND DAY(EEBDDY) > DAY(CompareDate)
                        )
                   THEN 1
                   ELSE 0
                 END ) 

SQL Fiddle

CREATE TABLE your_table
    ([EEBDCY] int, [EEBDYR] int, [EEBDMT] int, [EEBDDY] int)
;

INSERT INTO your_table
    ([EEBDCY], [EEBDYR], [EEBDMT], [EEBDDY])
VALUES
    (1900, 50, 3, 13),
    (1900, 50, 12, 31),
    (1900, 51, 3, 13),
    (1900, 51, 12, 31),
    (1900, 52, 3, 13),
    (1900, 52, 12, 31),
    (1900, 53, 3, 13),
    (1900, 53, 12, 31),
    (1900, 54, 3, 13),
    (1900, 54, 12, 31),
    (1900, 49, 3, 13),
    (1900, 49, 12, 31),
    (1900, 64, 3, 13),
    (1900, 64, 12, 31)
;

查询1

select
      *
      , YEAR(CompareDate) - (EEBDCY + EEBDYR)
            - ( CASE
                   WHEN MONTH(EEBDMT) > MONTH(CompareDate)
                     OR ( MONTH(EEBDMT) = MONTH(CompareDate)
                        AND DAY(EEBDDY) > DAY(CompareDate)
                        )
                   THEN 1
                   ELSE 0
                 END ) as Age
from your_table
cross apply (
    select DATEFROMPARTS((EEBDCY + EEBDYR), EEBDMT, EEBDDY), dateadd(month,3,getdate())
    ) ca (Birthdate, CompareDate)
where 65 <= YEAR(CompareDate) - (EEBDCY + EEBDYR)
            - ( CASE
                   WHEN MONTH(EEBDMT) > MONTH(CompareDate)
                     OR ( MONTH(EEBDMT) = MONTH(CompareDate)
                        AND DAY(EEBDDY) > DAY(CompareDate)
                        )
                   THEN 1
                   ELSE 0
                 END ) 
order by birthdate

<强> Results

| EEBDCY | EEBDYR | EEBDMT | EEBDDY |  Birthdate |              CompareDate | Age |
|--------|--------|--------|--------|------------|--------------------------|-----|
|   1900 |     49 |      3 |     13 | 1949-03-13 | 2018-03-06T03:14:52.793Z |  69 |
|   1900 |     49 |     12 |     31 | 1949-12-31 | 2018-03-06T03:14:52.793Z |  69 |
|   1900 |     50 |      3 |     13 | 1950-03-13 | 2018-03-06T03:14:52.793Z |  68 |
|   1900 |     50 |     12 |     31 | 1950-12-31 | 2018-03-06T03:14:52.793Z |  68 |
|   1900 |     51 |      3 |     13 | 1951-03-13 | 2018-03-06T03:14:52.793Z |  67 |
|   1900 |     51 |     12 |     31 | 1951-12-31 | 2018-03-06T03:14:52.793Z |  67 |
|   1900 |     52 |      3 |     13 | 1952-03-13 | 2018-03-06T03:14:52.793Z |  66 |
|   1900 |     52 |     12 |     31 | 1952-12-31 | 2018-03-06T03:14:52.793Z |  66 |
|   1900 |     53 |      3 |     13 | 1953-03-13 | 2018-03-06T03:14:52.793Z |  65 |
|   1900 |     53 |     12 |     31 | 1953-12-31 | 2018-03-06T03:14:52.793Z |  65 |