| Author |
Topic |
|
PurpleSun
Yak Posting Veteran
50 Posts |
Posted - 2007-05-22 : 14:44:36
|
| Hi All,I have two tables Table1( Caseid int, SrartDate datetime, EndDate datetime, EpisodeNumber int)insert into Table1select 1, '01/01/2007', '02/12/2007', 1 union allselect 1, '03/01/2007', '03/30/2007', 2 union allselect 2, '03/11/2007', '04/15/2007', 1 union allselect 3, '03/12/2007', '03/22/2007', 1 union allselect 3, '04/20/2007', '04/29/2007', 2 union allselect 4, '05/02/2007', '05/15/2007', 1Table2( Caseid int, ScanDate datetime)insert into Table2select 1, '03/29/2007' union allselect 2, '03/08/2007' union allselect 3, '04/01/2007' union allselect 4, '05/20/2007'These tables can be joined by CaseID.I need to select CaseID and EpisodeNumber from Table1where Table2.ScanDate falls between Table1.StartDate and Table1.EndDate, which is easy. My problem is when ScanDate doesn't fall between Start and EndDate.I need the following result from the query:CaseID EpisodeNumber1----------22----------13----------14----------1Thanks for your help |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-05-22 : 14:56:01
|
Something like this?Select t2.caseid, t1.episodenumberfrom Table2 t2join Table1 t1 on t1.Caseid = t2.Caseid and NOT (t2.ScanDate between t1.SrartDate and t1.EndDate ) Dinakar NethiSQL Server MVP************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
PurpleSun
Yak Posting Veteran
50 Posts |
Posted - 2007-05-22 : 15:10:51
|
| Dinakar,Thanks for the reply, but this doesn't solve my problem.Your query will not return the result I needed:CaseID EpisodeNumber1----------22----------13----------14----------1 |
 |
|
|
rudesyle
Posting Yak Master
110 Posts |
Posted - 2007-05-22 : 15:29:19
|
| The query you asked for would look like select a.Caseid,a.EpisodeNumberfrom @Table1 ainner join @Table2 b on a.CaseID=b.CaseIDwhere b.ScanDate not between a.SrartDate and a.EndDateand return1 12 13 13 24 1 |
 |
|
|
PurpleSun
Yak Posting Veteran
50 Posts |
Posted - 2007-05-22 : 15:35:11
|
| This query does not return the result I need.Resultset should include both cases: when scan date falls between start and end datesand when it doesn't.Here is what I need to be returned:1 22 13 14 1 |
 |
|
|
rudesyle
Posting Yak Master
110 Posts |
Posted - 2007-05-22 : 15:40:06
|
quote: Originally posted by PurpleSun This query does not return the result I need.Resultset should include both cases: when scan date falls between start and end datesand when it doesn't.Here is what I need to be returned:1 22 13 14 1
You just said you want to return the results where scan date falls between start and end date, and when it doesn't?????Wouldn't every record qualify then? |
 |
|
|
PurpleSun
Yak Posting Veteran
50 Posts |
Posted - 2007-05-22 : 15:49:15
|
| Sorry, I didn't clear it out. When scan date falls between start and end dates it should return particular EpisodeNumber for the case. The rest of records with the same CaseID should not be included in the result set. (As it is with CaseID=1)If scan date doesn't fall in between then it should return the nearest previous EpisodeNumber(like with CaseID=3 and CaseID=4).If previous EpisodeNumber does not exist the it should return the next EpisodeNumber (like with CaseID=2) |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-23 : 01:36:41
|
| hope episode numbers are in sequence...try this(try some other query whose cost is less when compared to this query)Select a.caseid, EpisodeNumber = case when EpisodeNumber is null then (select case when count(*) > 1 Then max(EpisodeNumber) -1 else max(EpisodeNumber) end from @t1 where caseid = a.caseid group by caseid) else EpisodeNumber end from @t2 a left outer join @t1 b on a.caseid = b.caseid and a.scandate between b.SrartDate and b.EndDate |
 |
|
|
PurpleSun
Yak Posting Veteran
50 Posts |
Posted - 2007-05-23 : 09:23:34
|
| Thanks a lot, it solves my problem |
 |
|
|
|