I have a table like this:
Id Name parentId -------------------------------- 5 Rollers 2 2 Paint and Brushes 1 1 Decorating NULL
Using PIVOT or any other single query, can I get an output like this:
cat1id cat1name cat2id cat2name cat3id cat3Name ------------------------------------------------------------------------ 1 Decorating 2 Paint and Brushes 5 Rollers
You can use
PIVOT,UNPIVOTand a recursive query to perform this.Static Version, is where you hard-code the values to the transformed:
See SQL Fiddle with Demo
Dynamic Version, the values are generated at run-time:
See SQL Fiddle with Demo
The Results are the same for both: