I'm having trouble with dynamic pivot. I tried to follow this expample: SQL Server : dynamic pivot over 5 columns but not getting results I want.
Data looks like below
AccDate Account AccountDescription Amount VatValue CalcLevel CalcObject
2020-08-31 4101 Purchaces 33750 4725 First 8309
2020-08-31 4101 Purchaces 33750 4725 Second 110
2020-08-31 4101 Purchaces 33750 4725 Third 111
2020-08-31 2660 VAT 4725 0 NULL NULL
With this Query
select AccDate, AccountDescription, Amount, VatValue, 'First' AS [CalcLevel1], 'Second' AS [CalcLevel2], 'Third' AS[CalcLevel3], '8309' AS [CalcObject1], '110' AS [CalcObject2], '111' AS [CalcObject3]
from
(
SELECT
AccDate,
Account,
AccountDescription,
Amount,
VatValue,
CalcLevel
FROM MyTable WHERE CalcLevel IS NOT NULL
) a
pivot
(
MIN(CalcLevel) for Account in ([First], [Second], [Third], [8309], [110], [111])
) as pvt;
I got result, which is correct
AccDate AccountDescription Amount VatValue CalcLevel1 CalcLevel2 CalcLevel3 CalcObject1 CalcObject2 CalcObject3
2020-08-31 Purchaces 33750 4725 First Second Third 8309 110 111
But what I really need is dynamic pivot (there are multiple Calclevels and CalcObjects). So the result should be like
AccDate AccountDescription Amount VatValue CalcLevel1 CalcLevel2 CalcLevel3 CalcLevel[n] CalcObject1 CalcObject2 CalcObject3 CalcObject[n]
2020-08-31 Purchaces 33750 4725 First Second Third result[n] 8309 110 111 result[n]
I tried this query below, but got nowhere so far. Is it possible to get result I described with dynamic pivot?
declare @sql nvarchar(max)
declare @cols nvarchar(max)
declare @sql nvarchar(max)
declare @cols nvarchar(max)
select @cols = STUFF((SELECT ',' + QUOTENAME(+col+CalcLevel)+','+QUOTENAME(col+CalcObject)
from mytable t
cross apply
(
select 'CalcLevel'
union all
select 'CalcObject'
) c (col)
group by col, CalcLevel, CalcObject
order by CalcLevel, CalcObject
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @cols
set @sql =
'SELECT AccDate, AccountDescription, Amount, VatValue, ' + @cols + '
FROM
( select AccDate, AccountDescription, Account, Amount, VatValue, CalcLevel,
col = CalcLevel+''_''+CalcObject+''_''+col,
value
from mytable t
cross apply
(
select ''CalcLevel'', CalcLevel
union all
select ''CalcObject'', CalcObject
) c (col, value)
) AS s
PIVOT
(
min(CalcLevel)
FOR Account IN (' + @cols + ')
) AS pvt
'
EXEC(@sql)