PIVOT with varchar datatype

后端 未结 1 1453
滥情空心
滥情空心 2021-01-11 11:12

I´m trying to PIVOT some data in a table, but I cannot do it because I do not find the way to do it using varchar columns. I have this table:

相关标签:
1条回答
  • 2021-01-11 11:44

    You can still use the PIVOT function to get the result but since you are aggregating a varchar you have to use either max or min:

    SELECT *
    FROM
    (
      SELECT [c_id]
          ,[c_lname] as [Apellido]
          ,[c_fname] as [Nombre]
          ,[c_nick_name] as [documento]      
          ,[ut_text] 
          ,f.ug_label
      FROM [pegasys].[dbo].[cardholder] c
      inner join [pegasys].[dbo].[udftext] u on c.c_id = u.ut_cardholder_id 
      inner join [pegasys].[dbo].[udfgen] f on u.ut_udfgen_id = f.ug_id
    ) d  
    PIVOT
    (
        max(ut_text)
        FOR UG_LABEL IN ([Torre], [Cuit], [Empresa], [Departamento])
    ) p
    
    0 讨论(0)
提交回复
热议问题