Hello,
I need help on how to find ID (below) where Code=5 existed for 5 or more consecutive months from 11/2005 to 11/2006 and change to Code=2 immediately following the consecutive month. Here is a sample data:
ID Code Beg_dt End_dt
1 5 20051201 20051231
1 5 20060101 20060131
1 5 20060201 20060531
1 2 20060601 20060831
2 5 20051101 20051130
2 5 20051201 20051231
2 2 20060101 20060331
2 5 20060401 20061130
ID=1 met the criteria because code=5 existed from 12/1/2005 to 5/31/2006 (6month) and changed to code 2 on 6/1/2006.
ID=2 doesn't met the criteria because code=5 existed only from 11/1/2005 to 12/31/2005 prior to code=2.
Here is the code to create the data in SQL 2000:
SELECT 1 AS [ID], 5 AS CODE, '20051201' AS BEG_DT, '20051231' AS EFF_END_DT
INTO MY_TABLE
UNION ALL
SELECT 1, 5, '20060101','20060131'
UNION ALL
SELECT 1, 5, '20060201', '20060531'
UNION ALL
SELECT 1, 2, '20060601', '20060831'
UNION ALL
SELECT 2, 5, '20051101', '20051130'
UNION ALL
SELECT 2, 5, '20051201', '20051231'
UNION ALL
SELECT 2, 2, '20060101', '20060331'
UNION ALL
SELECT 2, 5, '20060401', '20061130'
Thank you for all your assistance.
Chaoma
I need help on how to find ID (below) where Code=5 existed for 5 or more consecutive months from 11/2005 to 11/2006 and change to Code=2 immediately following the consecutive month. Here is a sample data:
ID Code Beg_dt End_dt
1 5 20051201 20051231
1 5 20060101 20060131
1 5 20060201 20060531
1 2 20060601 20060831
2 5 20051101 20051130
2 5 20051201 20051231
2 2 20060101 20060331
2 5 20060401 20061130
ID=1 met the criteria because code=5 existed from 12/1/2005 to 5/31/2006 (6month) and changed to code 2 on 6/1/2006.
ID=2 doesn't met the criteria because code=5 existed only from 11/1/2005 to 12/31/2005 prior to code=2.
Here is the code to create the data in SQL 2000:
SELECT 1 AS [ID], 5 AS CODE, '20051201' AS BEG_DT, '20051231' AS EFF_END_DT
INTO MY_TABLE
UNION ALL
SELECT 1, 5, '20060101','20060131'
UNION ALL
SELECT 1, 5, '20060201', '20060531'
UNION ALL
SELECT 1, 2, '20060601', '20060831'
UNION ALL
SELECT 2, 5, '20051101', '20051130'
UNION ALL
SELECT 2, 5, '20051201', '20051231'
UNION ALL
SELECT 2, 2, '20060101', '20060331'
UNION ALL
SELECT 2, 5, '20060401', '20061130'
Thank you for all your assistance.
Chaoma