I have data in table as below :
Three Columns
date1 fromdate value
---------------------
14 0 150
14 2 150
14 3 150
14 4 120
14 5 120
14 6 150
14 7 150
I need to write sql query to get data in below format:
Date1 fromdate todate value
----------------------------
14 0 3 150
14 4 5 120
14 6 7 150
Is this possible to do with SQL query in oracle ?
You need to split your rows with the value 150 into two separate sections. You can do this by using analytic functions. The
leadfunction allows you to look at the value of the next row by some order. In this case you want to find when you have a different value order by your fromdate:You can then compare this to the current value to see when they change, outputting a discriminator when they do:
You then need to “fill out” the discriminator to replace the nulls. This can be done with analytics again, selecting the
mindiscriminator value by fromdate descending. You can then group by this value to give you the output you want: