如何转置数据

时间:2018-03-07 15:30:17

标签: sql sql-server sql-server-2008 tsql

我试图将此表中的数据放在一行中,如下所示:

Incident  | Location | Order_Num |Item       |Shift     | Shift 2       |Shift 3 |Team_Member 1 |Team_Member 2  |Team_Member 3 |Team_Mem_ID 1|  Team_Mem_ID 2|  Team_Mem_ID 3|

M21-R17-1D| North_Am |948427     |T16_Term_GN|  2       |Weekend        |    | Chris Smith    |Sergei Skripal  | Megan Cohen   |248         |   1184         |   1662        |

这可以通过PIVOT或UNPIVOT完成吗?我的环境是MS SQL 2008.

创建表/数据的代码:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Sample_Table](
    [Incident] [varchar](20) NULL,
    [Location] [varchar](20) NULL,
    [Order_Num] [varchar](10) NULL,
    [Item] [varchar](30) NULL,
    [Shift] [varchar](20) NULL,
    [Team_Member] [varchar](101) NULL,
    [Team_Mem_ID] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Sample_Table] ([Incident], [Location], [Order_Num], [Item], [Shift], [Team_Member], [Team_Mem_ID]) VALUES (N'M21-R17-1D', N'North_Am', N'948427', N'T16_Term_GN', N'2', N'Chris Smith', N'248')
INSERT [dbo].[Sample_Table] ([Incident], [Location], [Order_Num], [Item], [Shift], [Team_Member], [Team_Mem_ID]) VALUES (N'M21-R17-1D', N'North_Am', N'948427', N'T16_Term_GN', N'2', N'Sergei Skripal', N'1184')
INSERT [dbo].[Sample_Table] ([Incident], [Location], [Order_Num], [Item], [Shift], [Team_Member], [Team_Mem_ID]) VALUES (N'M21-R17-1D', N'North_Am', N'948427', N'T16_Term_GN', N'2', N'Megan Cohen', N'1662')
INSERT [dbo].[Sample_Table] ([Incident], [Location], [Order_Num], [Item], [Shift], [Team_Member], [Team_Mem_ID]) VALUES (N'M21-R17-1D', N'North_Am', N'948427', N'T16_Term_GN', N'Weekend', N'Chris Smith', N'248')
INSERT [dbo].[Sample_Table] ([Incident], [Location], [Order_Num], [Item], [Shift], [Team_Member], [Team_Mem_ID]) VALUES (N'M21-R17-1D', N'North_Am', N'948427', N'T16_Term_GN', N'Weekend', N'Sergei Skripal', N'1184')
INSERT [dbo].[Sample_Table] ([Incident], [Location], [Order_Num], [Item], [Shift], [Team_Member], [Team_Mem_ID]) VALUES (N'M21-R17-1D', N'North_Am', N'948427', N'T16_Term_GN', N'Weekend', N'Megan Cohen', N'1662')

1 个答案:

答案 0 :(得分:1)

您可以使用PIVOT完成此操作,但我不推荐它。我建议使用APPLY,因为它允许您更容易地旋转或取消多列的旋转。

这是我的解决方案:

SELECT 
  Incident        = MAX(Incident),
  [Location]      = MAX([Location]),
  Order_Num       = MAX(Order_Num),
  [Shift]         = MAX(CASE [shift] WHEN '2' THEN [shift] END),
  [Shift2]        = MAX(CASE [shift] WHEN 'weekend' THEN [shift] END),
  [Shift3]        = MAX(CASE [shift] WHEN '3' THEN [shift] END),
  [Team_Member 1] = MAX(CASE rnk WHEN 1 THEN Team_Member END),
  [Team_Member 2] = MAX(CASE rnk WHEN 2 THEN Team_Member END),
  [Team_Member 3] = MAX(CASE rnk WHEN 3 THEN Team_Member END),
  [Team_Mem_ID 1] = MAX(CASE rnk WHEN 1 THEN Team_Mem_ID END),
  [Team_Mem_ID 2] = MAX(CASE rnk WHEN 2 THEN Team_Mem_ID END),
  [Team_Mem_ID 3] = MAX(CASE rnk WHEN 3 THEN Team_Mem_ID END)
FROM 
(
  SELECT t.*, rnk = DENSE_RANK() OVER (ORDER BY CAST(team_mem_id AS int))
  FROM dbo.sample_table t
) t
GROUP BY Incident, [Location], Order_Num;

返回:

Incident      Location   Order_Num  Shift   Shift2   Shift3  Team_Member 1   Team_Member 2   Team_Member 3  Team_Mem_ID 1  Team_Mem_ID 2  Team_Mem_ID 3
------------- ---------- ---------- ------- -------- ------- --------------- --------------- -------------- -------------- -------------- -------------
M21-R17-1D    North_Am   948427     2       Weekend          Chris Smith     Sergei Skripal  Megan Cohen    248            1184           1662

请注意,我假设您需要Team_mem_id 1,2,& 3按Team_mem_id排序。如果它确实是一个int,那么你应该将该列更新为int。