Blog Of Sem: SQL SERVER PIVOT TABLE SINIRSIZ COLUMN

SQL SERVER PIVOT TABLE SINIRSIZ COLUMN

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(ADI)
            FROM ZV_LOGO_MALZEME  GROUP BY ADI
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')

set @query = 'SELECT KODU,' + @cols + ' from
            (
                select KODU,ADI
                from ZV_LOGO_MALZEME
           ) x
            pivot
            (
                COUNT(ADI)
                for ADI in (' + @cols + ')
            ) p
           '

execute(@query)

--------------

İKİ FARKLI TABLODAN
DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(DEFINITION_)
            FROM LG_SLSMAN  GROUP BY DEFINITION_
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')

set @query = 'SELECT STGRPCODE,' + @cols + ' from
            (
                select LG_013_ITEMS.STGRPCODE,DEFINITION_
                from LG_SLSMAN
CROSS JOIN  LG_013_ITEMS where LG_013_ITEMS.STGRPCODE<>''''

           ) x
            pivot
            (
                MIN(DEFINITION_)
                for DEFINITION_ in (' + @cols + ')
            ) p
           '

execute(@query)