Author |
Topic |
cardgunner
326 Posts |
Posted - 2008-06-19 : 15:43:36
|
I need to find any record that may share the same date in a date range. A better word may be overlapping.What records overlap the other.declare @rcd_id varchar(3)declare @s_date datetimedeclare @e_date datetimeselect 'A' rcd_id,'05/01/2008' s_date,'05/21/2008'e_date union allselect 'A' rcd_id,'04/15/2008' s_date,'05/15/2008'e_date union allselect 'B' rcd_id,'05/01/2008' s_date,'05/21/2008'e_date union allselect 'B' rcd_id,'05/15/2008' s_date,'06/01/2008'e_date union allselect 'C' rcd_id,'05/05/2008' s_date,'05/21/2008'e_date union allselect 'C' rcd_id,'05/24/2008' s_date,'05/31/2008'e_date union allselect 'D' rcd_id,'05/02/2008' s_date,'05/20/2008'e_date union allselect 'A' rcd_id,'05/03/2008' s_date,'05/10/2008'e_date union allselect 'B' rcd_id,'06/03/2008' s_date,'06/21/2008'e_date union allselect 'A' rcd_id,'06/15/2008' s_date,'06/21/2008'e_date RESULTSrcd_id s_date e_date ------ ---------- ---------- A 05/01/2008 05/21/2008A 04/15/2008 05/15/2008B 05/01/2008 05/21/2008B 05/15/2008 06/01/2008C 05/05/2008 05/21/2008C 05/24/2008 05/31/2008D 05/02/2008 05/20/2008A 05/03/2008 05/10/2008B 06/03/2008 06/21/2008A 06/15/2008 06/21/2008(10 row(s) affected)DESIRED RESULTSRCD_ID CNT ------ ---- A 3B 2 I need to identify which records overlap by the id.I have thousands and thousands of records to search to find the ones that overlap. CardGunner |
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-06-19 : 16:50:25
|
[code]if object_id('tempdb..#table') is not nulldrop table #tablecreate table #table ( i int identity, rcd_id CHAR(1), s_date datetime, e_date datetime )insert #table ( rcd_id, s_date, e_date )select rcd_id, s_date, e_date from YourTableselect t2.rcd_id, count(*) as cntfrom ( select * from #table ) t1join ( select * from #table ) t2 on t1.rcd_id = t2.rcd_id and t1.i < t2.i and ( t2.s_date between t1.s_date and t1.e_date or t2.e_date between t1.s_date and t1.e_date )group by t2.rcd_id[/code] |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 08:11:48
|
That gave me rcd_id cnt ------ ----------- A 3 B 1 Which may work. I'm trying it now with my real tablesCardGunner |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 09:18:49
|
The only hiccup I've found is if the start date on of one record is the same as the end date then it counts it as an overlap. Other then that it seems to be working great with only 10 out of 111 tested there are 6 bonafide overlaps and 4 sames dates.Thank you very much. If you can come up with a way not count if the start date of one and the end of the other are the same please let me know. But the result set I have is workable. Thank you. Saved me a ton of time.CardGunner |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 09:39:49
|
I addedand t2.s_date<>t1.e_date to make the statement like this select t2.rcd_id, count(*) as cntfrom ( select * from #table ) t1join ( select * from #table ) t2 on t1.rcd_id = t2.rcd_id and t1.i < t2.i and ( t2.s_date between t1.s_date and t1.e_date or t2.e_date between t1.s_date and t1.e_date ) and t2.s_date<>t1.e_date group by t2.rcd_id It eliminated the records where the start and end was the same.I'm not a pro at this, obviously.Does anyone see a problem with the ending to eliminate the same dates?CardGunner |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 10:08:28
|
[code]SELECT t1.rcd_id,count(*) + 1 FROM Table t1INNER JOIN Table t2ON t2.rcd_id=t1.rcd_idAND (t2.s_date <=t1.s_date AND t2.e_date >t1.s_date)OR (t2.s_date<=t1.e_date AND t2.s_date >t1.s_date)GROUP BY t1.rcd_id[/code] |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 11:27:01
|
Sorry Visakh but yours doesn't get me the reults.This is what I used SELECT t1.rcd_id,count(*) + 1 FROM ( select * from tmptable ) t1 inner JOIN (select * from tmptable ) t2ON t2.rcd_id=t1.rcd_idAND (t2.s_date <=t1.s_date AND t2.e_date >t1.s_date)OR (t2.s_date<=t1.e_date AND t2.s_date >t1.s_date)GROUP BY t1.rcd_idsample resultscnt rcd_id ----------- ---------------- 70 21014 715 21665 268 22068 85 22356 72 23501 712 23578 767 23842 792 23939 1152 24139 1060 25044 545 25480 912 25648 520 25858 2454 26124 (926 row(s) affected) Somethings not rightCardGunner |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 11:43:58
|
[code]SELECT t1.rcd_id,count(*) + 1 FROM Table t1INNER JOIN Table t2ON t2.rcd_id=t1.rcd_idAND (t2.s_date <=t1.s_date AND t2.e_date >t1.s_date)OR (t2.s_date<=t1.e_date AND t2.e_date >t1.s_date)GROUP BY t1.rcd_id[/code] |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 12:21:17
|
That is 941 records.if I change the last to be AND (t2.s_date <=t1.s_date AND t2.e_date >t1.s_date OR t2.s_date<=t1.e_date AND t2.e_date >t1.s_date) that is 926 records.If I add and t1.i<t2.i that is 108 recordsCardGunner |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 13:55:41
|
quote: Originally posted by cardgunner That is 941 records.if I change the last to be AND (t2.s_date <=t1.s_date AND t2.e_date >t1.s_date OR t2.s_date<=t1.e_date AND t2.e_date >t1.s_date) that is 926 records.If I add and t1.i<t2.i that is 108 recordsCardGunner
What's the count you're expecting? |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 14:29:53
|
Sorry, 111 was the number.Counting that if the end date of a record was the same as the start date of another record of the same rcd_id then it counted it as a overlap when in fact it is not. So I added to the where clause of jdaman's example and t2.s_date<>t1.e_date to get 44.Out of those 44 I have looked up half and those 22 records are bonafide overlaps.Now I'm trying to figure out how to exclude the one or more records that are overlapping and keep the one I want. To keep the one that is correct.The key is trying to define which one do you want to keep. what seperates that one from the others.Arggghhhh!I was not sure, and still not sure if my additional clause was hiding something I may be wanting to count unintionally cause I'm not a pro at this. Kind of like counting a year as 365 without considering the leap year day, you know what I mean?CardGunner |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-20 : 14:41:54
|
[code]DECLARE @Sample TABLE (rcd_id VARCHAR(3), s_date DATETIME, e_date DATETIME)SET DATEFORMAT MDYINSERT @SampleSELECT 'A', '05/01/2008', '05/21/2008' UNION ALLSELECT 'A', '04/15/2008', '05/15/2008' UNION ALLSELECT 'B', '05/01/2008', '05/21/2008' UNION ALLSELECT 'B', '05/15/2008', '06/01/2008' UNION ALLSELECT 'C', '05/05/2008', '05/21/2008' UNION ALLSELECT 'C', '05/24/2008', '05/31/2008' UNION ALLSELECT 'D', '05/02/2008', '05/20/2008' UNION ALLSELECT 'A', '05/03/2008', '05/10/2008' UNION ALLSELECT 'B', '06/03/2008', '06/21/2008' UNION ALLSELECT 'A', '06/15/2008', '06/21/2008'SELECT rcd_id AS RCD_ID, COUNT(*) AS CNTFROM ( SELECT DISTINCT s1.rcd_id, s1.s_date, s1.e_date FROM @Sample AS s1 INNER JOIN @Sample AS s2 ON s2.rcd_id = s1.rcd_id WHERE s2.e_date >= s1.s_date AND s2.s_date <= s1.e_date AND s1.s_date <> s2.s_date AND s1.e_date <> s2.e_date ) AS dGROUP BY rcd_idORDER BY rcd_id[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-20 : 15:02:04
|
Maybe better in case of duplicates.Same sample data as above.SELECT s1.rcd_id AS RCD_ID, COUNT(*) AS CNTFROM @Sample AS s1WHERE EXISTS ( SELECT * FROM @Sample AS s2 WHERE s2.rcd_id = s1.rcd_id AND s2.e_date >= s1.s_date AND s2.s_date <= s1.e_date AND s1.s_date <> s2.s_date AND s1.e_date <> s2.e_date )GROUP BY s1.rcd_idORDER BY s1.rcd_id E 12°55'05.25"N 56°04'39.16" |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 16:08:47
|
Thanks Peso but both of yours counted two records of the same rcd_id as being overlapping but none did however one record for that tcd_id start date and one record for the end date was the same. So I'm assuming it counted them as overlapping. I could not see not other overlap. Here is the sample from the first one I tested.t_clot t_rsdt t_rrdt ---------------- ------------------------------------------------------ ------------------------------------------------------ 23330 2007-05-10 12:00:00.000 2007-06-22 12:00:00.00023330 2007-06-25 12:00:00.000 2007-06-28 12:00:00.00023330 2007-07-11 12:00:00.000 2007-07-12 12:00:00.00023330 2007-07-12 12:00:00.000 2007-07-18 12:00:00.00023330 2007-07-23 12:00:00.000 2007-07-30 12:00:00.00023330 2007-08-21 12:00:00.000 2007-10-16 12:00:00.00023330 2007-10-23 12:00:00.000 2007-11-27 13:00:00.00023330 2008-01-14 13:00:00.000 2008-01-21 13:00:00.000 Both of your statements counted this as 2.CardGunner |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-20 : 16:17:25
|
The two overlapping records arercd_id s_date e_date rcd_id s_date e_date------ ----------------------- ----------------------- ------ ----------------------- -----------------------23330 2007-07-12 12:00:00.000 2007-07-18 12:00:00.000 23330 2007-07-11 12:00:00.000 2007-07-12 12:00:00.00023330 2007-07-11 12:00:00.000 2007-07-12 12:00:00.000 23330 2007-07-12 12:00:00.000 2007-07-18 12:00:00.000 E 12°55'05.25"N 56°04'39.16" |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 16:19:04
|
Peso Your did count one that was correct that was not on jdaman's and the one that it counted was overlapped.HmmCardGunner |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 16:36:01
|
quote: Originally posted by Peso The two overlapping records arercd_id s_date e_date rcd_id s_date e_date------ ----------------------- ----------------------- ------ ----------------------- -----------------------23330 2007-07-12 12:00:00.000 2007-07-18 12:00:00.000 23330 2007-07-11 12:00:00.000 2007-07-12 12:00:00.00023330 2007-07-11 12:00:00.000 2007-07-12 12:00:00.000 23330 2007-07-12 12:00:00.000 2007-07-18 12:00:00.000 E 12°55'05.25"N 56°04'39.16"
Not for me they don't. One record started 7/11 at 12:00 and ended 7/12 at 12:00. The other started 7/12 at 12:00 and ended 7/18.I guess 2 records can't occupy the same exact time?In my world they can and I because of that I do not want to count them as overlapping.Also I found 3 records in yours that I did not have before. But I have at least 18 that I consider to be over lapping.Thanks for the alternative.CardGunner |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-20 : 16:38:48
|
So please inform us about your business rules when it comes to overlapping. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-20 : 16:40:49
|
[code]DECLARE @Sample TABLE (rcd_id VARCHAR(5), s_date DATETIME, e_date DATETIME)SET DATEFORMAT MDYINSERT @SampleSELECT '23330', '2007-05-10 12:00:00.000', '2007-06-22 12:00:00.000' UNION ALLSELECT '23330', '2007-06-25 12:00:00.000', '2007-06-28 12:00:00.000' UNION ALLSELECT '23330', '2007-07-11 12:00:00.000', '2007-07-12 12:00:00.000' UNION ALLSELECT '23330', '2007-07-12 12:00:00.000', '2007-07-18 12:00:00.000' UNION ALLSELECT '23330', '2007-07-23 12:00:00.000', '2007-07-30 12:00:00.000' UNION ALLSELECT '23330', '2007-08-21 12:00:00.000', '2007-10-16 12:00:00.000' UNION ALLSELECT '23330', '2007-10-23 12:00:00.000', '2007-11-27 13:00:00.000' UNION ALLSELECT '23330', '2008-01-14 13:00:00.000', '2008-01-21 13:00:00.000'SELECT *FROM @Sample AS s1INNER JOIN @Sample AS s2 ON s2.rcd_id = s1.rcd_idWHERE s2.e_date >= s1.s_date AND s2.s_date <= s1.e_date AND s1.s_date <> s2.s_date AND s1.e_date <> s2.e_dateSELECT rcd_id AS RCD_ID, COUNT(*) AS CNTFROM ( SELECT DISTINCT s1.rcd_id, s1.s_date, s1.e_date FROM @Sample AS s1 INNER JOIN @Sample AS s2 ON s2.rcd_id = s1.rcd_id WHERE s2.e_date > s1.s_date AND s2.s_date < s1.e_date AND s1.s_date <> s2.s_date AND s1.e_date <> s2.e_date ) AS dGROUP BY rcd_idORDER BY rcd_idSELECT s1.rcd_id AS RCD_ID, COUNT(*) AS CNTFROM @Sample AS s1WHERE EXISTS ( SELECT * FROM @Sample AS s2 WHERE s2.rcd_id = s1.rcd_id AND s2.e_date > s1.s_date AND s2.s_date < s1.e_date AND s1.s_date <> s2.s_date AND s1.e_date <> s2.e_date )GROUP BY s1.rcd_idORDER BY s1.rcd_id[/code]I have removed "=" in the comparisons. E 12°55'05.25"N 56°04'39.16" |
 |
|
cardgunner
326 Posts |
Posted - 2008-06-20 : 17:05:23
|
Thanks Peso.Sorry for any confusion. CardGunner |
 |
|
|