1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Sql generate column name with number in pivot

Discussão em 'Outras Linguagens' iniciado por Stack, Abril 23, 2021.

  1. Stack

    Stack Membro Participativo

    I don't exactly know how to articulate this question, so apology if this has been asked before. Here I have this query to pivot a table:

    SELECT *
    FROM
    ( SELECT
    [cashValue],
    [Name],
    [t]
    FROM
    [DBName].[dbo].[TableName]) AS SourceTable
    PIVOT (max([cashValue])
    FOR [t] IN ([1],[2],[3])
    ) AS PivotTable


    Notice I have the "[1],[2],[3]" when I need to generate the column name? My [t] has value spanning from 1 to 51. Can I just write a loop to generate [1] all the way to [51] ?

    Thanks

    Continue reading...

Compartilhe esta Página