简体   繁体   中英

unpivot a sql (columns to rows)

I have a table with 7 columns and I am trying to unpivot to rows. All the Columns starting P1Start should be a new column in new Unpivoted table. The table should like sample_Unpivot table: P1Start, P1End, P2Start, P2End ... should be a row value. I have attached the insert script below. Can you please provide me the code?

CREATE TABLE [dbo].[sample](
 [id] [int] NULL,
 [P1StartOrderDate] [datetime] NULL,
 [P1StartReceiveDate] [datetime] NULL,
 [P1EndOrderDate] [datetime] NULL,
 [P1EndReceiveDate] [datetime] NULL,
 [P2StartOrderDate] [datetime] NULL,
 [P2StartReceiveDate] [datetime] NULL,
 [P2EndOrderDate] [datetime] NULL,
 [P2EndReceiveDate] [datetime] NULL
) 

CREATE TABLE [dbo].[sample_unpivot](
 [id] [int] NULL,
 [Phase] [char](30) NULL,
 [OrderDate] [datetime] NULL,
 [ReceiveDate] [datetime] NULL
)  

INSERT [dbo].[sample] ([id], [P1StartOrderDate], [P1StartReceiveDate], [P1EndOrderDate], [P1EndReceiveDate], [P2StartOrderDate], [P2StartReceiveDate], [P2EndOrderDate], [P2EndReceiveDate]) VALUES (1, CAST(N'2020-01-01 00:00:00.000' AS DateTime), CAST(N'2020-01-03 00:00:00.000' AS DateTime), CAST(N'2020-05-01 00:00:00.000' AS DateTime), CAST(N'2010-01-01 00:00:00.000' AS DateTime), CAST(N'2020-08-01 00:00:00.000' AS DateTime), CAST(N'2020-01-11 00:00:00.000' AS DateTime), CAST(N'2020-05-03 00:00:00.000' AS DateTime), CAST(N'2020-01-04 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P1Start  ', CAST(N'2020-01-01 00:00:00.000' AS DateTime), CAST(N'2020-01-03 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P1End  ', CAST(N'2020-05-01 00:00:00.000' AS DateTime), CAST(N'2020-01-01 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P2Start  ', CAST(N'2020-08-01 00:00:00.000' AS DateTime), CAST(N'2020-01-11 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P2End  ', CAST(N'2020-05-03 00:00:00.000' AS DateTime), CAST(N'2020-01-04 00:00:00.000' AS DateTime))
GO

Your table structure suggests SQL Server syntax, so i would do :

select s.id, ss.*
from sample s cross apply
     ( values ('P1Start', P1StartOrderDate, P1StartReceiveDate),
              ('P1End', P1EndOrderDate, P1EndReceiveDate),
              ('P2Start', P2StartOrderDate, P2StartReceiveDate),
              ('P2End', P2EndOrderDate, P2EndReceiveDate)
     ) ss(Phase, OrderDate, ReceiveDate);

Another way you can try to use UNION ALL to make it.

select id,'P1Start' Phase, P1StartOrderDate OrderDate, P1StartReceiveDate ReceiveDate
from sample s
UNION ALL
select id,'P1End' Phase,P1EndOrderDate, P1EndReceiveDate
from sample s
UNION ALL
select id,'P2Start' Phase, P2StartOrderDate, P2StartReceiveDate
from sample s
UNION ALL
select id,'P2End' Phase,  P2EndOrderDate, P2EndReceiveDate
from sample s

sqlfiddle

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