SQL Server:根据条件

时间:2016-11-28 01:20:07

标签: sql-server

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

  1. 查询需要根据日期检索最适用的记录
  2. 对于内部日期范围内的日期,将返回与CurrentDate最接近的记录
  3. 对于内部日期范围之外的日期,将返回与CurrentDate最接近的记录
  4. 数据库中的示例表:

    表:

    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,根据上述条件返回记录?

1 个答案:

答案 0 :(得分: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