sql-server – SQL Server:根据条件查找与CurrentDate最接近的日期的记录

我正在使用SQL Server 2012,我正在尝试创建一个VIEW,它将根据这些条件返回记录:

>查询需要根据日期检索最适用的记录
>对于内部日期范围内的日期,将返回与CurrentDate最接近的记录
>对于内部日期范围之外的日期,将返回与CurrentDate最接近的记录

数据库中的示例表:

人员表:

pId     | Name
----------------------
01      | Person 1
02      | Person 2
----------------------

PersonDate表:

dId     |  pId      | StartDate     | EndDate
---------------------------------------------------
A1      |   01      |   2014-01-08  |   2018-01-08  
A2      |   01      |   2016-11-23  |   2016-12-01  
A3      |   01      |   2016-12-03  |   2016-12-08
A4      |   02      |   2016-10-10  |   2016-12-31
A5      |   02      |   2016-12-01  |   2016-12-05

如果我运行此查询并且CurrentDate是2016-11-28:

select p.name, d.startdate, d.enddate
from Person p, PersonDate d
where p.pId = d.pId
and d.StartDate = (select max(sl.StartDate)
                   from PersonDate sl
                   where d.pId = s1.pId)

返回的记录是:

name        | startdate     | enddate
-------------------------------------------
Person 1    |   2016-12-03  |   2016-12-08      --> PersonDate Table row A3
Person 2    |   2016-12-01  |   2016-12-05      --> PersonDate Table row A5
-------------------------------------------

根据我想要回来的条件,两个返回的记录都是不正确的.我理解为什么我得到返回的记录,这是由于在我的子查询中使用Max()函数,但我不知道如何编写查询/子查询.

我想要返回的正确记录是(CurrentDate是2016-11-28):

name        | startdate     | enddate
-------------------------------------------
Person 1    |   2016-11-23  |   2016-12-01
Person 2    |   2016-10-10  |   2016-12-31
-------------------------------------------

> PersonDate表行A2,因为此内部日期范围最接近CurrentDate(条件#2)
> PersonDate表格行A4,因为内部日期范围(A5)尚未到来(条件#3)

当CurrentDate是2016-12-02时:

name        | startdate     | enddate
---------------------------------------------
Person 1    |   2014-01-08  |   2018-01-08  
Person 2    |   2016-12-01  |   2016-12-05
---------------------------------------------

> PersonDate表行A1,因为CurrentDate在行A2和A3内部日期范围之外
> PersonDate表行A5,因为CurrentDate在日期范围内

如何编写一个基于上述条件返回记录的VIEW?

最佳答案

create table #temp(did varchar(10),pid int,startdate datetime,enddate datetime)

insert into #temp values('A1',01,'2014-01-08','2018-01-08')
insert into #temp values('A2',01,  '2016-11-23'  ,   '2016-12-01'   )
insert into #temp values('A3',01, '2016-12-03'  ,   '2016-12-08'  )
insert into #temp values('A4',02,  '2016-10-10'  ,   '2016-12-31'  )
insert into #temp values('A5',02, '2016-12-01'  ,   '2016-12-05'  )


select b.pid,b.startdate,b.enddate
from
(
select ROW_NUMBER()over(partition by pid order by id desc) as SID , a.*
from
(
select 
ROW_NUMBER()over(partition by pid order by startdate,enddate desc) as ID
, * from #temp 
--to identify whether it is inner or outer
--1 means outer
--2 means inner
)a
where '2016-12-02' between startdate and enddate
--to find date lies in outer or inner range and select the required
)b
where b.SID=1
点赞