EDUDOTNET

Showing posts with label Pivot table. Show all posts
Showing posts with label Pivot table. Show all posts

Monday, December 14, 2015

Pivot table with single column in sql server

- Create Table

CREATE TABLE MyTable
([Id] int)
;

-Insert Data In to Table

INSERT INTO MyTable
([Id])
VALUES
(1),
(2),
(3),
(4),
(5)
;


-Design a query with Pivot 

select *
from
(
  select  CAST(Id AS VARCHAR(15)) +'Col' AS Name , Id AS UserId
  from MyTable
) d
pivot
(
     Min(UserId)
    FOR [Name] IN ([1Col], [2Col],[3Col],[4Col], [5Col])
) piv;