Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2003-09-24 : 12:58:04
|
| Hi guys,Consider the following :KEY.......EFFDATE.......CATEGORY1.........01/10/1996....B 1.........01/11/1996....LC1.........01/01/2003....A What the above 3 records represent is a person being part of a category, or in pensions (joins a benefit). It is obvious from the above that the person has joined (or switched benefits) twice. He started off in B then switched to LC and finally settled on A.What I want to have is something like the following :KEY.......EFFDATE.......CATEGORY1.........01/10/1996....Joins B1.........31/10/1996....leaves B1.........01/11/1996....Joins LC1.........31/12/2002....Leaves LC1.........01/01/2003....Joins ANote that the data that I am dealing with involves many members with some members having one single benefit (nothing needs to be done to those as they will only have one record showing them as having joined the benefit).Here is some sample data for you to help me with this query.[CODE]CREATE TABLE BENEFITS (MEMBERKEY INT , EFFDATE CHAR(10), CATEGORY CHAR(2))INSERT INTO BENEFITS(MEMBERKEY, EFFDATE, CATEGORY)SELECT 1,'01/10/1996', 'B' UNION ALLSELECT 1,'01/11/1996','LC' UNION ALLSELECT 1,'01/01/2003','A' UNION ALLSELECT 5,'02/07/1990','B' UNION ALLSELECT 5,'01/11/1992','LC' UNION ALLSELECT 5,'01/01/2003','A' UNION ALLSELECT 6,'24/09/1990','B' UNION ALLSELECT 6,'01/01/1991','LC' UNION ALLSELECT 6,'01/01/2003','A' [/CODE]Thanks for your help in advance.__________________Make love not war! |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-09-24 : 13:22:55
|
| Something likeselect Key, EffDate, Category = 'Joins ' + Categoryfrom BENEFITSunion allselect t1.Key, EffDate = dateadd(dd,-1,t2.EffDate), Category = 'Leaves ' + t1.Categoryfrom BENEFITS t1join BENEFITS t2on t1.Key = t2.Keyand t2.EffDate = (select min(t3.EffDate) from BENEFITS t3 where t1.Key = t3.Key and t3.EffDate > t1.EffDateorder by Key, EffDate==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2003-09-25 : 04:51:00
|
| Thank you very much nr. Your solution works very well.Much appreciated.__________________Make love not war! |
 |
|
|
|
|
|
|
|