I am new to this forum. Hope fully I will be able to contribute and get my queries resolved too. I am stuck at this that I do not know where to start off.
I have below data set.
| Start Step| End 1 | End 2 |
| 1001866 | 1001867 | NULL |
| 1001866 | 1001868 | NULL |
| 1001868 | 1001873 | NULL |
| 1001873 | 1001868 | NULL |
| 1001868 | 1005206 | NULL |
| 1001873 | 1001867 | NULL |
| 1005206 | 1001873 | NULL |
| 1005206 | 1005385 | 1005386 |
| 1005206 | 1005377 | 1005378 |
| 1005378 | 1005376 | 1005206 |
| 1005379 | 1005376 | 1005206 |
| 1005379 | 1005380 | 1005381 |
| 1005381 | 1005382 | 1001869 |
| 1005381 | 1005383 | NULL |
| 1005381 | 1005384 | 1001872 |
| 1005378 | 1005379 | NULL |
| 1005383 | 1001872 | NULL |
| 1005383 | 1005376 | 1005206 |
| 1005383 | 1005381 | NULL |
| 1001869 | 1001871 | NULL |
| 1005386 | 1005376 | 1005206 |
I want each step to be in single row with their corresponding end1 and end2 and ordered by step and ranked. I want the output to be as in the image:
| Rank | Start | End Step 1 | End Step 2 |
| 1 | 1001866 | 1001867 | NULL |
| 1 | 1001866 | 1001868 | NULL |
| 2 | 1001867 | NULL | NULL |
| 3 | 1001868 | 1001873 | NULL |
| 3 | 1001868 | 1005206 | NULL |
| 4 | 1001869 | NULL | NULL |
| 4 | 1001869 | 1001871 | NULL |
| 5 | 1001871 | NULL | NULL |
| 6 | 1001872 | NULL | NULL |
| 7 | 1001873 | 1001868 | NULL |
| 7 | 1001873 | 1001867 | NULL |
| 8 | 1005206 | 1001873 | NULL |
| 8 | 1005206 | 1005385 | 1005386 |
| 8 | 1005206 | 1005377 | 1005378 |
| 9 | 1005376 | NULL | NULL |
| 10 | 1005377 | NULL | NULL |
| 11 | 1005378 | 1005379 | NULL |
| 11 | 1005378 | 1005376 | 1005206 |
| 12 | 1005379 | 1005376 | 1005206 |
| 12 | 1005379 | 1005380 | 1005381 |
| 13 | 1005380 | NULL | NULL |
| 14 | 1005381 | 1005382 | 1001869 |
| 14 | 1005381 | 1005383 | NULL |
| 14 | 1005381 | 1005384 | 1001872 |
| 15 | 1005382 | NULL | NULL |
| 16 | 1005383 | 1001872 | NULL |
| 16 | 1005383 | 1005376 | 1005206 |
| 16 | 1005383 | 1005381 | NULL |
| 17 | 1005384 | NULL | NULL |
| 18 | 1005385 | NULL | NULL |
| 19 | 1005386 | 1005376 | 1005206 |
| 19 | 1005386 | 1005387 | NULL |
| 20 | 1005387 | NULL | NULL |
Just highlighted few values for better understanding.
Is it possible ? Can any one please help ?
select dense_rank() over(order by [start step]) [rank], * from
(select * from yourtable
union
select distinct [end 1], null, null from yourtable where [end 1] is not null
union
select distinct [end 2], null, null from yourtable where [end 2] is not null
)a order by [start step]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.