Advertisement
Guest User

Untitled

a guest
Nov 27th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.29 KB | None | 0 0
  1. With CTE as(
  2. select a.*,
  3.        max(b.num) parent_id
  4.   from accounts a
  5.   left join accounts b on b.num<a.num and b.Hierarchy_level<a.Hierarchy_level
  6.   group by a.num,a.Account_code,a.Account_alias,a.Hierarchy_level
  7. )
  8. select c.*,d.num as child_id
  9.   from CTE c, CTE d where c.num=d.parent_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement