【问题标题】:SQL Server: Find records with closest Date to CurrentDate based on conditionsSQL Server:根据条件查找与 CurrentDate 最接近的记录
【发布时间】:2016-11-28 15:40:51
【问题描述】:

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

  1. 查询需要根据日期检索最适用的记录
  2. 对于内部日期范围内的日期,将返回最接近 CurrentDate 的记录
  3. 对于内部日期范围之外的日期,将返回最接近 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 来根据上述条件返回记录?

【问题讨论】:

  • 上述条件2和3的结果是不是一样(返回最接近CurrentDate的记录)?
  • “内部日期范围”是指每条记录的开始日期到结束日期范围吗?
  • 写得很好的问题。在您的示例数据中,在我看来,A4 也只是条件 2,因为它在日期范围内?找到“最接近”日期的解决方案是找到两个日期之间的天数差,然后使用ABS 删除负数,并找到最小的数字。
  • 不,条件 2 基于内部日期范围,条件 3 基于不在任何内部日期范围内...我很难弄清楚如何描述这一点。如果您查看 PersonDate 表:CGritton:A1 是主要日期范围,A2 和 A3 是人员 1 的内部日期范围。如果 CurrentDate 在 A2 或 A3 内,则满足条件 2,如果当前日期不在,则满足条件 3 t 在 A2 或 A3 内。希望能解决这个问题。最后 2 个示例应该有助于解释这 2 个条件。
  • Nick McDermaid:A4 是第 2 个人的主要日期范围,A5 是第 2 个人的内部日期范围。CGritton:“内部日期范围”是主要日期范围的子日期范围。 A1 和 A4 是主要日期范围,而 A2、A3、A5 是各自 Person 对象的子日期范围。

标签: sql-server


【解决方案1】:
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

【讨论】:

  • Mani:谢谢,为了满足我的数据库需求,我们做了一些非常小的修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 2019-03-29
  • 2018-05-30
相关资源
最近更新 更多