Tuesday, May 8, 2012

Using xml path to combine multiple rows into single column


Can combine multiple row values into a single column using xml path function

select distinct employee_manager_id,
(SELECT ',' + h1.employee_id from hr.dim_employee_reporting h1
where h1.employee_manager_id=h.employee_manager_id for xml path('')) as employees
from hr.dim_employee_reporting h
where employee_manager_id = 'xxxxx'

Source reference: http://bisqlserver.rdacorp.com/2011/10/using-for-xml-path-to-combine-multiple.html

No comments: