shragi
asked on
SQL Retrieve Values
Hi - My data is like below
Col1 COL2 col3
A 12-Apr-13 12-APR-16 09.27.20.000000000 AM
B 12-Apr-13 12-APR-16 09.27.34.000000000 AM
A 12-Apr-13 13-APR-16 09.27.34.000000000 AM
A 14-May-13 15-APR-16 09.17.31.000000000 AM
B 14-May-13 15-APR-16 09.17.38.000000000 AM
A 22-May-13 28-APR-16 09.24.14.000000000 AM
B 4-Jun-13 10-MAY-16 11.28.24.000000000 AM
A 14-Jun-13 25-MAY-16 09.30.45.000000000 AM
B 22-Jun-13 26-MAY-16 11.00.25.000000000 AM
Now what I am trying to do is show the results if there is A then show B
output table is like below i mapped
col1 - Astatus, Bstatus
col2 - Adate, Bdate
AStatus ADate BStatus BDate
A 12-Apr-13 B 12-Apr-13
A 12-Apr-13 null null
A 14-May-13 B 14-May-13
If you observe my output you can see in second line that there is null values in Bstatus and Bdate reason for that is in the input table the row 4 is A and not B, I am expecting B after each A if I did not get it then I will store null values.
My approach for this problem is
Sort the input table based on col3 and create a cursor to get each row after sorted
then check the values of each row and set the values in output table
If my procedure is wrong let me know or else if there is a simple procedure let me know.
I wrote the cursor but not sure how to do select from this
Col1 COL2 col3
A 12-Apr-13 12-APR-16 09.27.20.000000000 AM
B 12-Apr-13 12-APR-16 09.27.34.000000000 AM
A 12-Apr-13 13-APR-16 09.27.34.000000000 AM
A 14-May-13 15-APR-16 09.17.31.000000000 AM
B 14-May-13 15-APR-16 09.17.38.000000000 AM
A 22-May-13 28-APR-16 09.24.14.000000000 AM
B 4-Jun-13 10-MAY-16 11.28.24.000000000 AM
A 14-Jun-13 25-MAY-16 09.30.45.000000000 AM
B 22-Jun-13 26-MAY-16 11.00.25.000000000 AM
Now what I am trying to do is show the results if there is A then show B
output table is like below i mapped
col1 - Astatus, Bstatus
col2 - Adate, Bdate
AStatus ADate BStatus BDate
A 12-Apr-13 B 12-Apr-13
A 12-Apr-13 null null
A 14-May-13 B 14-May-13
If you observe my output you can see in second line that there is null values in Bstatus and Bdate reason for that is in the input table the row 4 is A and not B, I am expecting B after each A if I did not get it then I will store null values.
My approach for this problem is
Sort the input table based on col3 and create a cursor to get each row after sorted
then check the values of each row and set the values in output table
If my procedure is wrong let me know or else if there is a simple procedure let me know.
DECLARE
CURSOR c1 IS
SELECT col1,col2,col3 FROM input_table
ORDER BY col3 ASC;
my_col3 date;
my_col1 VARCHAR2(20);
my_col2 date;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO my_col1 , my_col2 , my_col3 ;
EXIT WHEN c1%NOTFOUND;
COMMIT;
END LOOP;
CLOSE c1;
END;
I wrote the cursor but not sure how to do select from this
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select astatus, adate,
decode(bstatus,astatus,nul
decode(bstatus,astatus,nul
from
(select col1 as astatus, col2 as adate,
lead(col1) over (order by col3, col1) as bstatus,
lead(col2) over (order by col3, col1) as bdate
from yourtable)
where astatus = 'A';