SQLTeam.com | Weblogs | Forums

(Pivot) Convert Row to Column


#1

hello guys, i i've problem to use Pivot
here is my query :

;WITH CTE AS 
(
      SELECT ,NAME,JOB_LEVEL_POSITION,DIGITAL_SIGN
            DENSE_RANK() OVER (ORDER BY SEQUENCE DESC) AS RN
      FROM data_employee
)
SELECT * FROM CTE
PIVOT (MAX(DIGITAL_SIGN) FOR RN IN ([1],[2],[3],[4],[5],[6],[7],[8]))P

and the result :

NAME JOB_POSITION 1   2   3   4
BRO    DEV	XXXX NULL NULL NULL
Tony   QA	 NULL XXXD NULL NULL
Momo   MGR	 NULL NULL YYYY NUL

L

the problem is,
i need to select column like this :

my data : 
Data Employee
    Name         Position        Sign    Sequence
    Bro               DEV         XXXX        1
    Tony              QA          XXXD        2
    Momo              MGR        YYYY         3

and i want the result like this:

 Name1 Name2 Name3  Position1 Position2 Position3   Sign1  Sign Sign3
 Bro   Tony   Momo      DEV      QA         MGR      XXXX  XXXD  YYYY 

thank you for your help


#3

sorry i dont understand, whats wrong with my post?


#4

select *
from (
select src.*
from @Table
cross apply(
select name, 'Name' + cast(Sequence as varchar)
union
select position, 'Position' + cast(Sequence as varchar)
union
select [sign], 'Sign' + cast(Sequence as varchar))src(col1, col2)
)Src
PIVOT (MAX(col1) FOR col2 IN ([Name1], [Name2], [Name3], [Position1], [Position2], [Position3], [Sign1], [Sign2], [Sign3]))pvt
edit for the formatting