简体   繁体   中英

Transposing the column in SQL server and removing unwanted columns?

I have a table like this:

Dimension Code      Dimension Value Code    Entry No_
BUSINESSSEGMENTS       153                     1
TERMINAL               41150                   1

I want to transpose it, for that I am using this query:

select  *
from realTable
unpivot (value for DimValCode in ([Dimension Value Code])) up
pivot (max(value) for [Dimension Code] in (BUSINESSSEGMENTS,TERMINAL)) p

and it's working fine and giving output like this:

Entry No_     DimValCode                   BUSINESSSEGMENTS     TERMINAL
   1          Dimension Value Code              153              41150

But I need this:(don't need DimValCode column)

Entry No_       BUSINESSSEGMENTS    TERMINAL
  1                  153             41150

What I need to modify in this query ?

Thanks.

Try like this,

SELECT [Entry No_]
    ,[BUSINESSSEGMENTS]
    ,[TERMINAL]
FROM (
    SELECT *
    FROM realTable
    unpivot(value FOR DimValCode IN ([Dimension Value Code])) up
    pivot(max(value) FOR [Dimension Code] IN (
                BUSINESSSEGMENTS
                ,TERMINAL
                )) p
    ) T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM