2016-11-28 9 views
4

に基づいてCURRENTDATEに最も近い日付のレコードを探す私はSQL Server 2012のを使用していると私は、これらの条件に基づいてレコードを返すVIEWを作成しようとしています:SQL Serverの:条件

  1. クエリする必要があります内側の日付範囲内にある日付の日付
  2. に基づいて、最も該当するレコードを取得、CURRENTDATEに最も近いレコードが内部日付範囲外にある日付の
  3. を返される、CURRENTDATEに最も近いレコードが
  4. を返されます
データベースで0

サンプル表:

テーブル:

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月2日である:CurrentDateの両方行A2及びA3インナー日外であるため、

name  | startdate  | enddate 
--------------------------------------------- 
Person 1 | 2014-01-08 | 2018-01-08 
Person 2 | 2016-12-01 | 2016-12-05 
--------------------------------------------- 
  • PersonDateテーブル行A1はCurrentDateであるので、
  • PersonDateテーブル行A5の範囲日付範囲内

上記の条件に基づいてレコードを返すVIEWを作成するにはどうすればよいですか?

+1

上記の条件2と3の結果は同じですか(CurrentDateに最も近いレコードが返されます)。 – CGritton

+0

「内部日付範囲」とは、各レコードの開始日から終了日までの範囲を意味しますか? – CGritton

+0

非常によく書かれた質問。あなたのサンプルデータでは、それは日付範囲内にあるので、A4はちょうど条件2であると私には思われますか? 「最も近い」日付を見つけることの解決策は、2つの日付間の日数の差異を見つけ、次に「ABS」を使ってネガを取り除き、最小の数を見つけることです。 –

答えて

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 
+0

Mani:ありがとう、これは私のデータベースのニーズに合うようにいくつかの非常に小さな変更を加えて動作しました。 – NotACoder