Skip to content
Advertisement

SQL query for hierarchical number

Here is the data:

And the result i want:

The clue is that each entry has own order number, but the query should returns hierarchical number which contains all the parent numbers as a subsequence delimited by the point with the own number at the end. The main feature requests an ability of providing a hierarchical number for any entry, so the query should be able to receive an ID of the entry and to return a hierarchical number of this. DBMS is MSSQL 2017. Thanks in advance!

Advertisement

Answer

You want a recursive CTE

It can be refactored as a TVF as well

db<>fiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement