【问题标题】:Return information from a Date Range with two criteria从具有两个条件的日期范围返回信息
【发布时间】:2016-10-11 10:25:33
【问题描述】:

我是 SQL 新手,我正在尝试从某个日期范围内提取数据。就我而言,我想要一周前的数据。我正在努力争取上周年满 13 岁的客户,但我的 between 条款不起作用。有什么建议吗?

select distinct [Account Number], [Date of Birth], [Age in Years]
from adhoc.[Patient Demographics] 
where [Age in Years] = 13 and between datepart(dd,getdate()) and datepart(dd,getdate()-7)

列是:

PracName (varchar(95), null)
Patient ID(int, not null)
Account number (varchar(15), null)
Title (varchar(50), null
First Name (Varchar(95), not null)
Middle Name (varchar(95), not null)
Last Name (varchare(95), not null)
Gender (varchar(1), null
Date of Birth(datetime, null)
Age in Years(int, null)
Age in Months (int, null)
Age in Weeks (int, null)
Age in Days (int, null)
Age in UDS (int, null)
Age Group (varchare(7), no null)

【问题讨论】:

    标签: sql tsql between date-range datepart


    【解决方案1】:

    请提供列字段。我相信您需要使用 OR 而不是 AND

    select distinct [Account Number], [Date of Birth], [Age in Years]
    from adhoc.[Patient Demographics] 
    where [Age in Years] = 13 OR[Age in Years] between datepart(dd,getdate()) and datepart(dd,getdate()-7)
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      select distinct [Account Number], [Date of Birth], [Age in Years]
      from adhoc.[Patient Demographics] 
      where [Age in Years] = 13 and DATEADD(YEAR, 13, [Date of Birth]) between DATEADD(DAY, -7, GETUTCDATE()) AND getdate() 
      

      我首先在当前的Date of Birth 上加上 13 年,然后检查结果日期是否是从现在起过去 7 天。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-27
        • 2017-08-17
        相关资源
        最近更新 更多