SQL Server:根据条件查找日期与当前日期最接近的记录

2024-04-02

我正在使用 SQL Server 2012,并尝试创建一个将根据以下条件返回记录的视图:

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

数据库中的示例表:

Person table:

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

人物日期 table:

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)

When CurrentDate是 2016 年 12 月 2 日:

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在日期范围内

我怎样才能编写一个根据上述条件返回记录的视图?


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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SQL Server:根据条件查找日期与当前日期最接近的记录 的相关文章

随机推荐