SQL将在2017年、2018年或2019年为列范围内的每行返回1

SQL将在2017年、2018年或2019年为列范围内的每行返回1,sql,date,sql-server-2008-r2,range,Sql,Date,Sql Server 2008 R2,Range,我有一个如下的查询: SELECT DISTINCT p.person_ID ,p.Last_Name ,ISNULL(p.Middle_Initial, '') AS Middle ,p.First_Name ,sh.Status_from_date ,sh.Status_thru_date --(a) FROM person p INNER JOIN Person_Facilities f ON p.

我有一个如下的查询:

SELECT DISTINCT
     p.person_ID
     ,p.Last_Name 
     ,ISNULL(p.Middle_Initial, '') AS Middle
     ,p.First_Name
     ,sh.Status_from_date
     ,sh.Status_thru_date
     --(a)
FROM 
    person p
INNER JOIN 
    Person_Facilities f ON p.Person_ID = f.Person_ID
LEFT OUTER JOIN
    rv_person_status_hist sh ON p.person_ID = sh.person_ID
ORDER BY 
    Last_Name
返回的数据看起来像这样(暂时忽略2018列):

我如何添加一列,比如[2018],如果状态从状态到状态在2018年,我如何添加一个1,如果状态不在2018年,我如何添加一个0

我想在查询中的--(a)处添加以下内容:

,(SELECT case 
             when exists
             (
                select *  --
                FROM   dbo.RV_Person_status_hist
                where 
                status_from_date is not null
                and
                ('1-1-2018' between status_from_date and status_thru_date)
                and status_from_date is not null
                ) 
                then 1 else 0 end  ) 

                  AS [2018]
不过,这似乎不起作用。见上表中的2018列。它显示所有返回的值为1,并且不排除空值。这很复杂。status_from和status_thru可能随2018年而下降,或者2018年可能在status_from和status_thru内部,两者都应为1

当状态日期包括2018年时,如何排除空值,以及如何显示1

我已经看过了,还有。我不认为我有所有的案例,因为范围也重叠

**更新: 我试着在上面的--(a)处添加这个,根据下面的潜在答案:

,(SELECT status_from_date, status_thru_date,
             case 
             when datepart(year, status_from_date)='2018'
               or datepart(year, status_thru_date)='2018'
               or (
                status_from_date <= '01/01/2018'
                and status_thru_date >= '12/12/2018'
               )
            then 1
              else 0
              end)  AS [2018]
,(从日期选择状态,从日期选择状态,
案例
当datepart(年份,状态从日期开始)='2018'
或日期部分(年份、状态到日期)='2018'
或(
自日期起的状态='12/12/2018'
)
那么1
其他0
完)截至[2018]
但我得到了模棱两可的列名“status\u from\u date”。模棱两可的 列名“状态到日期”。只能指定一个表达式 在“选择”列表中,当子查询未引入时存在

有什么想法吗?我明白了

**更新2:这个怎么样

,(case when (
                (
                    (sh.status_from_date is null or sh.status_from_date <= '2017-01-01') and
                    (sh.status_thru_date is null or sh.status_thru_date >= '2017-12-31')
                ) 
                or
                (
                    (f.status_from_date is null or f.status_from_date <= '2017-01-01') and
                    (f.status_thru_date is null or f.status_thru_date >= '2017-12-31')
                ) 
                or
                (
                    (datepart(year, sh.status_from_date)='2017') or
                    (datepart(year, sh.status_thru_date)='2017') or
                    (datepart(year, f.status_from_date)='2017') or
                    (datepart(year, f.status_from_date)='2017')

                ) 
                and
                    p.Sex='M'
            )
       then 1 else 0
end) as [2017_Male]
,(case when (
                (
                    (sh.status_from_date is null or sh.status_from_date <= '2017-01-01') and
                    (sh.status_thru_date is null or sh.status_thru_date >= '2017-12-31')
                ) 
                or
                (
                    (f.status_from_date is null or f.status_from_date <= '2017-01-01') and
                    (f.status_thru_date is null or f.status_thru_date >= '2017-12-31')
                ) 
                or
                (
                    (datepart(year, sh.status_from_date)='2017') or
                    (datepart(year, sh.status_thru_date)='2017') or
                    (datepart(year, f.status_from_date)='2017') or
                    (datepart(year, f.status_from_date)='2017')

                ) 
                and
                    p.Sex='F'
            )
       then 1 else 0
end) as [2017_Female]--------
,(在(
(
(sh.status_from_date为空或sh.status_from_date='2017-12-31')
) 
或
(
(f.status_from_date为空或f.status_from_date='2017-12-31')
) 
或
(
(datepart(年份,sh.status,自日期起)=‘2017’)或
(日期部分(年,上海州至日期)=‘2017’)或
(日期部分(年份,f.status_自_日期)=‘2017’)或
(日期部分(年份,f.status_自_日期)='2017')
) 
和
p、 性交
)
然后是1或0
完)作为[2017年男性]
,(如适用)(
(
(sh.status_from_date为空或sh.status_from_date='2017-12-31')
) 
或
(
(f.status_from_date为空或f.status_from_date='2017-12-31')
) 
或
(
(datepart(年份,sh.status,自日期起)=‘2017’)或
(日期部分(年,上海州至日期)=‘2017’)或
(日期部分(年份,f.status_自_日期)=‘2017’)或
(日期部分(年份,f.status_自_日期)='2017')
) 
和
p、 性class='F'
)
然后是1或0
完)作为[2017年11月1日]--------

在2017年的栏中,男性和女性的数据都是1,分别为:status_从:2014-10-01和status_到:2016-09-30

SELECT DISTINCT
     p.person_ID
     ,p.Last_Name 
     ,ISNULL(p.Middle_Initial, '') AS Middle
     ,p.First_Name
     ,sh.Status_from_date
     ,sh.Status_thru_date
     --(a)
FROM 
    person p
INNER JOIN 
    Person_Facilities f ON p.Person_ID = f.Person_ID
LEFT OUTER JOIN
    rv_person_status_hist sh ON p.person_ID = sh.person_ID
ORDER BY 
    Last_Name
而检查起始日期或结束日期是否包含2018年,或者日期是否介于起始日期和结束日期之间

CREATE TABLE #testTable (
    Status_from_date DATETIME,
    Status_thru_date DATETIME
    )

INSERT INTO #testTable (
    Status_from_date,
    Status_thru_date
    )
VALUES (
    '2017-06-01 00:00',
    '2019-05-31 00:00'
    ),
    (
    NULL,
    '2010-01-28 07:38'
    ),
    (
    '2018-07-01 00:00',
    '2020-06-30 00:00'
    )

SELECT Status_from_date,
    Status_thru_date,
    CASE 
        WHEN datepart(year, Status_from_date) = '2018'
            OR datepart(year, Status_thru_date) = '2018'
            OR (
                Status_from_date <= '01/01/2018'
                AND Status_thru_date >= '12/12/2018'
                )
            THEN 1
        ELSE 0
        END AS '2018'
FROM #testTable

DROP TABLE #testTable

你可以这样做:

SELECT DISTINCT
     p.person_ID
     ,p.Last_Name 
     ,ISNULL(p.Middle_Initial, '') AS Middle
     ,p.First_Name
     ,sh.Status_from_date
     ,sh.Status_thru_date
     --(a)
FROM 
    person p
INNER JOIN 
    Person_Facilities f ON p.Person_ID = f.Person_ID
LEFT OUTER JOIN
    rv_person_status_hist sh ON p.person_ID = sh.person_ID
ORDER BY 
    Last_Name
而检查起始日期或结束日期是否包含2018年,或者日期是否介于起始日期和结束日期之间

CREATE TABLE #testTable (
    Status_from_date DATETIME,
    Status_thru_date DATETIME
    )

INSERT INTO #testTable (
    Status_from_date,
    Status_thru_date
    )
VALUES (
    '2017-06-01 00:00',
    '2019-05-31 00:00'
    ),
    (
    NULL,
    '2010-01-28 07:38'
    ),
    (
    '2018-07-01 00:00',
    '2020-06-30 00:00'
    )

SELECT Status_from_date,
    Status_thru_date,
    CASE 
        WHEN datepart(year, Status_from_date) = '2018'
            OR datepart(year, Status_thru_date) = '2018'
            OR (
                Status_from_date <= '01/01/2018'
                AND Status_thru_date >= '12/12/2018'
                )
            THEN 1
        ELSE 0
        END AS '2018'
FROM #testTable

DROP TABLE #testTable

如果您想在2018年实现任何重叠,那么:

(case when (status_from_date is null or status_from_date < '2019-01-01') and
           (status_to_date is null or status_to_date >= '2018-01-01')
      then 1 else 0
 end) as is_2018
(当(自日期起的状态为空或自日期起的状态为<'2019-01-01'),以及
(状态至日期为空或状态至日期>='2018-01-01')
然后是1或0
完)与2018年一样
如果您想要完整年份的重叠:

(case when (status_from_date is null or status_from_date <= '2018-01-01') and
           (status_to_date is null or status_to_date >= '2018-12-31')
      then 1 else 0
 end) as is_2018
(以下情况下为空或状态自日期='2018-12-31')
然后是1或0
完)与2018年一样

如果您想在2018年实现任何重叠,那么:

(case when (status_from_date is null or status_from_date < '2019-01-01') and
           (status_to_date is null or status_to_date >= '2018-01-01')
      then 1 else 0
 end) as is_2018
(当(自日期起的状态为空或自日期起的状态为<'2019-01-01'),以及
(状态至日期为空或状态至日期>='2018-01-01')
然后是1或0
完)与2018年一样
如果您想要完整年份的重叠:

(case when (status_from_date is null or status_from_date <= '2018-01-01') and
           (status_to_date is null or status_to_date >= '2018-12-31')
      then 1 else 0
 end) as is_2018
(以下情况下为空或状态自日期='2018-12-31')
然后是1或0
完)与2018年一样

我添加了sql-server-2008-r2(在我们升级lol之前暂时不允许为空)您可以检查FROM=year start。我添加了sql-server-2008-r2(在我们升级lol之前暂时不允许为空)您可以检查FROM=year start。我试图将您的选择放在我的查询中的--(a)(请参阅**更新),由于某种原因,我从_date和thru中得到了错误的不明确列名status_…当子查询没有引入exists时,只能在select列表中指定一个表达式。有什么想法吗?我知道我为什么会犯这种含糊不清的错误。我忘了我的内连接f有一个重复的列,左外连接sh。这一列也包括在范围内的年份,所以我给出了答案。我试图将您的选择放在我的查询中的--(a)(请参见**更新),由于某种原因,我从_date和thru中得到了错误的不明确列名status_…当子查询没有引入exists时,只能在select列表中指定一个表达式。有什么想法吗?我知道我为什么会犯这种含糊不清的错误。我忘了我的内连接f有一个重复的列,左外连接sh。这一列也包括在范围内的年份,所以我给出了答案。我在--(a)尝试了第一个建议,我得到了模棱两可的列名“status_from_date”(也包括一个)。有什么想法吗?我知道我为什么会犯这种含糊不清的错误。我忘记了我的内部联接f有一个重复的列,带有左外部联接sh。起初它似乎是正确的,但当年份在范围内而不是在范围检查之外时,它就丢失了。@Michele。它应该能很好地处理这种情况。唯一的问题是你是否想要任何覆盖