| Author |
Topic |
|
klanda20
Starting Member
5 Posts |
Posted - 2011-10-12 : 15:47:17
|
| Hello All,I have a problem that is stumping me. I have records that have unique ID's and a start date and an end date. If a record has an end date that equals another record's start date I need to role that up into 1 "encounter".Basically the data looks like this....ID | start | end4 01/02/2011 01/05/20114 01/05/2011 01/11/20114 03/03/2011 03/04/20114 03/04/2011 03/06/2011So I need to somehow group distinguish group rows 1&2 from rows 3&4.Sorry if that it confusing....it's somewhat hard to verbalize. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-10-12 : 17:23:03
|
Does this help?DECLARE @Foo TABLE (ID INT, start DATE, [end] DATE)INSERT @FooVALUES(4, '01/02/2011', '01/05/2011'),(4, '01/05/2011', '01/11/2011'),(4, '03/03/2011', '03/04/2011'),(4, '03/04/2011', '03/06/2011')SELECT *FROM @Foo AS AINNER JOIN @Foo AS BON A.ID = B.IDAND A.[end] = B.start |
 |
|
|
klanda20
Starting Member
5 Posts |
Posted - 2011-10-13 : 07:40:58
|
| Lamprey,Thanks that does work in that scenario but I should have expanded my explanation. There can be multiple records that should roll up into one. See the example below.DECLARE @Foo TABLE (rn INT, ID INT, start DATE, [end] DATE)INSERT @FooVALUES(1, 4, '01/02/2011', '01/05/2011'),(2, 4, '01/05/2011', '01/11/2011'),(3, 4, '03/03/2011', '03/04/2011'),(4, 4, '03/04/2011', '03/06/2011'),(5, 4, '01/11/2011', '01/21/2011')SELECT *FROM @Foo AS AINNER JOIN @Foo AS BON A.ID = B.IDAND A.[end] = B.startIn this example row number 1,2 and 5 should roll up and row number 3 and 4 should roll up.Thanks again! |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-13 : 07:52:59
|
Can you show us what do you think how the output should look like? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
klanda20
Starting Member
5 Posts |
Posted - 2011-10-13 : 08:10:25
|
| Ultimately I would like to update the table and flag each group as so....rn id start end group1 4 2011-01-02 2011-01-05 12 4 2011-01-05 2011-01-11 13 4 2011-03-03 2011-03-04 24 4 2011-03-04 2011-03-06 25 4 2011-01-11 2011-01-21 1 |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-10-13 : 12:54:02
|
I didn't do the update, but this is one what that you might be able to group your rows (obviously replace * with column names):DECLARE @Foo TABLE (rn INT, ID INT, start DATE, [end] DATE)INSERT @FooVALUES(1, 4, '01/02/2011', '01/05/2011'),(2, 4, '01/05/2011', '01/11/2011'),(3, 4, '03/03/2011', '03/04/2011'),(4, 4, '03/04/2011', '03/06/2011'),(5, 4, '01/11/2011', '01/21/2011');WITH cte AS(SELECT A.*, A.rn AS ParentGroup, 1 AS LevelFROM @Foo AS AWHERE NOT EXISTS ( SELECT * FROM @Foo AS B WHERE A.ID = B.ID AND A.start = B.[end] )UNION ALL SELECT B.*, A.ParentGroup, A.Level + 1 FROM @Foo AS B INNER JOIN cte AS A ON B.ID = A.ID AND B.start = A.[end])SELECT *FROM cte EDIT: Here is another way using a left join instead of a NOT EXISTS, might be faster..?DECLARE @Foo TABLE (rn INT, ID INT, start DATE, [end] DATE)INSERT @FooVALUES(1, 4, '01/02/2011', '01/05/2011'),(2, 4, '01/05/2011', '01/11/2011'),(3, 4, '03/03/2011', '03/04/2011'),(4, 4, '03/04/2011', '03/06/2011'),(5, 4, '01/11/2011', '01/21/2011');WITH cte AS( SELECT A.*, A.rn AS ParentGroup, 1 AS Level FROM @Foo AS A LEFT OUTER JOIN @Foo AS B ON A.ID = B.ID AND A.start = B.[end]UNION ALL SELECT B.*, A.ParentGroup, A.Level + 1 FROM @Foo AS B INNER JOIN cte AS A ON B.ID = A.ID AND B.start = A.[end])SELECT *FROM cte |
 |
|
|
|
|
|