This is again out of my depth, therefore I need SQL Guru to help me again, Thanks guys in advance.
This is a fraction of a large client table with thousands of rows.
Client | Start Date | End Date | Key | Type | Sequence |
AAA | 2007-12-12 | 2008-05-31 | 85 | 40 | 1 |
AAA | 2009-06-01 | 2010-05-31 | 76 | 40 | 1 |
AAA | 2010-06-01 | 2011-05-31 | null | null | null |
AAA | 2011-06-01 | 2011-06-27 | null | null | null |
BBB | 2008-10-01 | 2009-09-30 | 163 | 40 | 1 |
BBB | 2009-10-01 | 2010-09-30 | null | null | null |
BBB | 2011-10-01 | 2012-09-30 | 180 | 40 | 1 |
BBB | 2012-10-01 | 2013-09-30 | null | null | null |
BBB | 2013-10-01 | 2014-09-30 | null | null | null |
I was asked to write a script to detect Type: 40 and manipulate data below it.
the end results looks like: taking the same Key as Type 40. and assign correct sequence
Client | Start Date | End Date | Key | Type | Sequence |
AAA | 2007-12-12 | 2008-05-31 | 85 | 40 | 1 |
AAA | 2009-06-01 | 2010-05-31 | 76 | 40 | 1 |
AAA | 2010-06-01 | 2011-05-31 | 76 | 20 | 2 |
AAA | 2011-06-01 | 2011-06-27 | 76 | 20 | 3 |
BBB | 2008-10-01 | 2009-09-30 | 163 | 40 | 1 |
BBB | 2009-10-01 | 2010-09-30 | 163 | 20 | 2 |
BBB | 2011-10-01 | 2012-09-30 | 180 | 40 | 1 |
BBB | 2012-10-01 | 2013-09-30 | 180 | 20 | 2 |
BBB | 2013-10-01 | 2014-09-30 | 180 | 20 | 3 |
Create table test1 (
[client] varchar(5) not null,
[startdate] datetime not null,
[enddate] datetime not null,
[policykey] int null,
[type] varchar(3) null,
[sequence] int null
)
Insert into test1 values (‘AAA’,’2007-12-12’, ‘2008-05-31’,85,’40’,1)
Insert into test1 values (‘AAA’,’2009-06-01’, ‘2010-05-31’,76,’40’,1)
Insert into test1 values (‘AAA’,’2010-06-01’, ‘2011-05-31’,null,null,null)
Insert into test1 values (‘AAA’,’2011-06-01’, ‘2011-06-27’,null,null,null)
Insert into test1 values (‘BBB’,’2008-10-01’,’2009-09-30’, 163, ‘40’, 1)
Insert into test1 values (‘BBB’,’2009-10-01’,’2010-09-30’, null,null,null)
Insert into test1 values (‘BBB’,’2011-10-01’,’2012-09-30’, 180, ‘40’, 1)
Insert into test1 values (‘BBB’,’2012-10-01’,’2013-09-30’, null,null,null)
Insert into test1 values (‘BBB’,’2013-10-01’,’2014-09-30’, null,null,null)