Hi,
I am using the following Dynamic PIVOT by Naomi,
Wow this is great, now what i want to do is add more columns
I can add the new Column, but cannot work out how to input the data, i have tried including another pivot joining etc, as i can do this in a static pivot, but this would be much better, and i have to do alot of these,
thanks to whom answers
SQL
DECLARE @MaxCount INT SELECT @MaxCount = max(cnt) FROM ( SELECT InpatientEpisodeIdFk ,count(Field3) as Cnt FROM [BIU].[zstg_RHHxRay_2] Where InpatientEpisodeIdFk >0 GROUP BY InpatientEpisodeIdFk ) X; DECLARE @SQL NVARCHAR(max) ,@i INT; SET @i = 0; WHILE @i < @MaxCount BEGIN SET @i = @i + 1; SET @SQL = COALESCE(@Sql + ', ', '') + 'xRay' + cast(@i AS NVARCHAR(100)); SET @SQL = COALESCE(@Sql + ', ', '') + 'DtXray' + cast(@i AS NVARCHAR(100)); END SET @SQL = N' ;WITH CTE AS ( SELECT InpatientEpisodeIdFk, Field4, ''xRay'' + CAST(row_number() OVER (PARTITION BY InpatientEpisodeIdFk ORDER BY Field3) AS Varchar(155)) AS RowNo FROM [BIU].[zstg_RHHxRay_2] ) SELECT * FROM CTE PIVOT (MAX(Field4)FOR RowNo IN (' + @SQL + N')) AS pvt1' ; PRINT @SQL; EXECUTE (@SQL);
Now this is what i am trying to do
David