Advertisement
Guest User

Untitled

a guest
Nov 27th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.44 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.*,(
  9.     SELECT Chars = STUFF((
  10.         SELECT ', ' + cast(d.num as varchar)
  11.           FROM CTE d where c.num=d.parent_id
  12.            FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '')
  13.     )
  14.   from CTE c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement