| Author |
Topic |
|
nev_sql
Starting Member
3 Posts |
Posted - 2006-06-27 : 07:37:09
|
| Hi,I have the following tableid nr GROUP DATE_BEGIN DATE_END TREATMENT1 1 3 1-1-2005 15-1-2005 1111112 1 3 14-1-2005 16-2-2005 2222223 1 3 14-1-2005 16-2-2005 1111114 1 3 17-3-2005 18-4-2005 1223125 1 3 18-5-2005 17-6-2005 3333336 2 4 17-1-2005 16-2-2005 3333337 2 4 15-2-2005 18-3-2005 458521The query should give as result only the rows with a none overlap or double period while nr and group are the same.So in this example i only want to see row 4 and 5.Thanx for your time.Nevil |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 07:48:46
|
So for the test data present, the wanted output isid nr GROUP DATE_BEGIN DATE_END TREATMENT4 1 3 17-3-2005 18-4-2005 1223125 1 3 18-5-2005 17-6-2005 333333 Ryan, are you up for it?Peter LarssonHelsingborg, Sweden |
 |
|
|
nev_sql
Starting Member
3 Posts |
Posted - 2006-06-27 : 07:58:52
|
| Yes that is correct. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 08:56:47
|
Hey, it works!-- Prepare test datadeclare @test table ([id] int, [nr] int, [GROUP] int, [DATE_BEGIN] datetime, [DATE_END] datetime, [TREATMENT] int)set dateformat dmyinsert @testselect 1, 1, 3, '1-1-2005', '15-1-2005', 111111 union allselect 2, 1, 3, '14-1-2005', '16-2-2005', 222222 union allselect 3, 1, 3, '14-1-2005', '16-2-2005', 111111 union allselect 4, 1, 3, '17-3-2005', '18-4-2005', 122312 union allselect 5, 1, 3, '18-5-2005', '17-6-2005', 333333 union allselect 6, 2, 4, '17-1-2005', '16-2-2005', 333333 union allselect 7, 2, 4, '15-2-2005', '18-3-2005', 458521-- Select the itemsselect distinct t2.*from @test t1inner join @test t2 on t2.nr = t1.nr and t2.[group] = t1.[group]where t1.date_end < t2.date_begin Peter LarssonHelsingborg, Sweden |
 |
|
|
nev_sql
Starting Member
3 Posts |
Posted - 2006-06-27 : 09:33:53
|
| It is working! thanx for your time! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 09:50:59
|
quote: Originally posted by nev_sql It is working! thanx for your time!
What about date intervals203 18-1-2006 22-2-2006708 22-2-2006 6-3-2006 Is that allowed? Should both rows 203 and 708 be output?If consecutive date intervals are treated as overlapping, use this alternative codeSELECT *FROM @testWHERE id NOT IN ( SELECT t1.id FROM @test t1 INNER JOIN @test t2 ON t2.nr = t1.nr AND t2.[group] = t1.[group] AND t1.id <> t2.id WHERE (t1.date_begin >= t2.date_begin AND t1.date_begin <= t2.date_end) or (t1.date_end >= t2.date_begin AND t1.date_end <= t2.date_end) ) If consecutive date intervals are treated as non-overlapping, use this codeSELECT *FROM @testWHERE id NOT IN ( SELECT t1.id FROM @test t1 INNER JOIN @test t2 ON t2.nr = t1.nr AND t2.[group] = t1.[group] AND t1.id <> t2.id WHERE (t1.date_begin > t2.date_begin AND t1.date_begin < t2.date_end) or (t1.date_end > t2.date_begin AND t1.date_end < t2.date_end) ) Try the different outputs with this test datadeclare @test table ([id] int, [nr] int, [GROUP] int, [DATE_BEGIN] datetime, [DATE_END] datetime, [TREATMENT] int)set dateformat dmyinsert @testselect 1, 1, 3, '1-1-2005', '15-1-2005', 111111 union allselect 2, 1, 3, '14-1-2005', '16-2-2005', 222222 union allselect 3, 1, 3, '14-1-2005', '16-2-2005', 111111 union allselect 4, 1, 3, '17-3-2005', '18-4-2005', 122312 union allselect 5, 1, 3, '18-5-2005', '17-6-2005', 333333 union allselect 6, 2, 4, '17-1-2005', '16-2-2005', 333333 union allselect 7, 2, 4, '15-2-2005', '18-3-2005', 458521 union allselect 8, 1, 5, '18-5-2005', '17-6-2005', 333333 union allselect 9, 1, 5, '17-6-2005', '16-7-2005', 333333 Peter LarssonHelsingborg, Sweden |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-27 : 12:58:40
|
quote: Originally posted by Peso So for the test data present, the wanted output isid nr GROUP DATE_BEGIN DATE_END TREATMENT4 1 3 17-3-2005 18-4-2005 1223125 1 3 18-5-2005 17-6-2005 333333 Ryan, are you up for it?Peter LarssonHelsingborg, Sweden
I am too slow Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-27 : 13:17:36
|
Is this the same thing?--datadeclare @test table ([id] int, [nr] int, [GROUP] int, [DATE_BEGIN] datetime, [DATE_END] datetime, [TREATMENT] int)set dateformat dmyinsert @testselect 1, 1, 3, '1-1-2005', '15-1-2005', 111111 union allselect 2, 1, 3, '14-1-2005', '16-2-2005', 222222 union allselect 3, 1, 3, '14-1-2005', '16-2-2005', 111111 union allselect 4, 1, 3, '17-3-2005', '18-4-2005', 122312 union allselect 5, 1, 3, '18-5-2005', '17-6-2005', 333333 union allselect 6, 2, 4, '17-1-2005', '16-2-2005', 333333 union allselect 7, 2, 4, '15-2-2005', '18-3-2005', 458521 union allselect 8, 1, 5, '18-5-2005', '17-6-2005', 333333 union allselect 9, 1, 5, '17-6-2005', '16-7-2005', 333333--calculationselect * from @test awhere not exists (select * from @test where nr = a.nr and a.[group] = [group] and not id = a.id and not (date_begin > a.date_end or date_end < a.date_begin))/*resultsid nr GROUP DATE_BEGIN DATE_END TREATMENT ----------- ----------- ----------- ------------------------------------------------------ ------------------------------------------------------ ----------- 4 1 3 2005-03-17 00:00:00.000 2005-04-18 00:00:00.000 1223125 1 3 2005-05-18 00:00:00.000 2005-06-17 00:00:00.000 333333*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2006-06-27 : 13:32:28
|
[code]"(t1.date_begin >= t2.date_begin AND t1.date_begin <= t2.date_end)or (t1.date_end >= t2.date_begin AND t1.date_end <= t2.date_end)"[/code]To find an overlap you only need to check:[code]t1.date_begin <= t2.date_end AND t1.date_end >= t2.date_begin[/code]rockmoose Edit:As Ryan just said...... Another rewrite:[code]SELECT t1.*FROM @test t1 LEFT JOIN @test t2 ON t2.nr = t1.nr AND t2.[group] = t1.[group] AND t1.id <> t2.id AND t1.date_begin <= t2.date_end AND t1.date_end >= t2.date_beginWHERE t2.Id IS NULL[/code] |
 |
|
|
|
|
|