Connecting Tech Pros Worldwide Help | Site Map

Need help urgently, have to compare dates between 2 rows in one table

Newbie
 
Join Date: Jul 2009
Posts: 2
#1: Jul 30 '09
Have to compare dates of two rows in one table
Input table
COL1| COL2| COL3| COL4
1| A| 1-Jan-07| 6-Feb-08
2| A| 7-Feb-08| 31-Mar-08
3| A| 1-Jan-09| NULL

SQL code to compare COL 4 date (Row 1) with COL 3 date (row 2)
and compare COL 4 date (Row 2) with COL 3 date (row 3)

If COL2 (ROW2) date is equal to COL 3 (ROW 1) + 1 = output Col 4 =A
If COL2 (ROW2) date is equal to COL 3 (ROW 1) + 2 = output Col 4 =R


OUTPUT table
COL1| COL2| COL3| COL4
A| 1-Jan-07|| 6-Feb-08| A
A| 7-Feb-08| 31-Mar-08| O
A| 1-Jan-09| NULL| R
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Jul 30 '09

re: Need help urgently, have to compare dates between 2 rows in one table


Here, tweak this...

Good luck!

--- CK
Newbie
 
Join Date: Jul 2009
Posts: 2
#3: Jul 30 '09

re: Need help urgently, have to compare dates between 2 rows in one table


Quote:

Originally Posted by site View Post

Have to compare dates of two rows in one table
Input table
COL1| COL2| COL3| COL4
1| A| 1-Jan-07| 6-Feb-08
2| A| 7-Feb-08| 31-Mar-08
3| A| 1-Jan-09| NULL

SQL code to compare COL 4 date (Row 1) with COL 3 date (row 2)
and compare COL 4 date (Row 2) with COL 3 date (row 3)

If COL3 (ROW2) date is equal to COL 4 (ROW 1) + 1 = output Col 4 =A
If COL3(ROW2) date is equal to COL 4 (ROW 1) + 2 = output Col 4 =R


OUTPUT table
COL1| COL2| COL3| COL4
A| 1-Jan-07|| 6-Feb-08| A
A| 7-Feb-08| 31-Mar-08| O
A| 1-Jan-09| NULL| R

Correction in Below query-Updated query
Reply