Convert Row to Column SQL server query

0

Good morning,

I need to transpose or change the result of a table in the following way:

The structure of the table is 5 columns (description of users, permission1, permission2, permission3, permission4)

the description field is varchar (50) and the fields permission1 to permission4 is Bit type

The table has a list of 10 users.

Usuarios    1   2   3   4
USUARIO1    X   X   X   
USUARIO2    X       X   X

Permisos    USUARIO 1   USUARIO2
1             X             X
2             X 
3             X             X
4                           X

Thank you.

    
asked by hector aristizabal 31.08.2017 в 19:01
source

1 answer

1

Using the Pivot function you can do what you want; look at the following example:

SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days, 
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost 
    FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;
    
answered by 01.09.2017 в 17:29