【发布时间】:2021-04-07 01:38:07
【问题描述】:
如何形成以下记录的管理层次?
输入数据:
| Id | Sub ID | Name | Description |
|---|---|---|---|
| 101 | NULL | Page Reference | Page Reference |
| 102 | 1 | Page 1 | Page 1 |
| 103 | 2 | Ashok | Ashok |
| 104 | 3 | Kumar | Kumar |
| 105 | 4 | Page 2 | Page 2 |
| 106 | 5 | Arvind | Arvind |
| 107 | 4 | Page 11 | Page 11 |
| 108 | 6 | Gova | Gova |
| 109 | 7 | Gokul | Gokul |
| 110 | 8 | Kannan | Kannan |
我尝试使用递归 CTE,但找不到确切的解决方案。需要以下格式, 条件是
New Leaf ID --> If Sub ID IS NULL , then it will be 1 , If contains page row, it will be 2, if contains other than that it will be 3.
Page --> Whenever Page row starts, from the next row original information showing for that page. we need to form based on the lead rows.
输出数据:
| Id | Sub ID | Name | Page | New Leaf ID |
|---|---|---|---|---|
| 101 | NULL | Page Reference | 1 | |
| 102 | 1 | Page 1 | Page 1 | 2 |
| 103 | 2 | Ashok | Page 1 | 3 |
| 104 | 3 | Kumar | Page 1 | 3 |
| 107 | 4 | Page 11 | Page 11 | 2 |
| 108 | 6 | Gova | Page 11 | 3 |
| 109 | 7 | Gokul | Page 11 | 3 |
| 110 | 8 | Kannan | Page 11 | 3 |
| 105 | 4 | Page 2 | Page 2 | 2 |
| 106 | 5 | Arvind | Page 2 | 3 |
【问题讨论】:
标签: sql sql-server sql-server-2012 case gaps-and-islands