I got child / parent tables as below.
MasterTable:
MasterID, Description
ChildTable
ChildID, MasterID, Description.
Using PIVOT / UNPIVOT how can i get result as below in single row.
if (MasterID : 1 got x child records)
MasterID, ChildID1, Description1, ChildID2, Description2....... ChildIDx, Descriptionx
Thanks
It greatly depends on whether the number of crosstabbed columns is fixed. If they are, then you can simply do something like:
There is also a way of achieving similar results use
CASE
functions.However, if what you want is to have the number of crosstab columns determined at runtime, then the only means to do that inside of SQL Server is to use some fugly dynamic SQL. This is outside the realm of SQL Server's primary purpose which is to serve up data (as opposed to information). If you want a dynamic crosstab, I would recommend not doing it in SQL Server but instead use a reporting tool or build your result set in a middle-tier component.
here is a T_SQL, assuming this:
DECLARE @MaxCountOfChild int
EDIT: The result is this: