SQL - 复杂的数据透视 - 获取多行数据并压缩成一行,行之间没有唯一键

时间:2014-07-30 23:39:10

标签: sql sql-server

这是我的数据

ID  LotNo       AlarmDateTime       AlarmMessage
1   1       1/1/2014 12:00      E-Stop
2   1       1/1/2014 12:02      Safety Door Opened
3   1       1/1/2014 12:05      Robot Fault
4   3       1/2/2014 5:09       Operator Door Opened
5   3       1/2/2014 6:00       Feeder Empty
6   5       1/4/2014 14:00      Feeder Jam

我想要的是这个:

LotNo,Alm_1_DT,Alm_1_MS,Alm_2_DT,Alm_2_ms,Alm_3_dt,Alm_3_ms
1, 1/1/2014 12:00, E-Stop, 1/1/2014 12:02, Safety Door Opened, 1/1/2014 12:05, Robot Fault
3, 1/2/2014 5:09, Operator Door Opened, 1/2/2014 6:00, Feeder Empty, NULL, NULL
5, 1/4/2014 14:00, Feeder Jam, NULL, NULL, NULL, NULL

我花了几天时间研究这个问题,我知道我可以用存储过程解决它并将其保存到表中,但这会是系统密集型的。我希望有一种方法可以使用select语句并构建视图。

感谢您的时间

Doc。

1 个答案:

答案 0 :(得分:0)

感谢Twelfth的建议,这提供了解决方案的第一部分,解决方案的第二部分是当我发现我可以使用分区重置row_number()命令时(找到解决方案here)通过这两个项目,我能够构建一个可以转动并获得所需输出的数据集。

select [Conveyor_ID], [1] as Alarm_1_1, [2] as Alarm_1_2, [3] as Alarm_2_1, [4] as Alarm_2_2, [5] as Alarm_3_1, 
                      [6] as Alarm_3_2, [7] as Alarm_4_1, [8] as Alarm_4_2, [9] as Alarm_5_1, [10] as Alarm_5_2
From
(
    select [Conveyor_ID], Data, row_Number() over (partition by [conveyor_ID] order by getdate() ) as IndexNum
    from (Select top 100 percent [Conveyor_ID], cast([Alarm_Message] as varchar(60)) as [Alarm_Message], cast([Alarm_datetime] as varchar(60)) as [Alarm_datetime] 
          from [Database].[dbo].[tabel]
          order by [conveyor_ID], [Report_Alarm_ID] asc) RA
    unpivot (Data for Type in ([Alarm_datetime], [Alarm_Message])) as upvt
) as TB1
pivot
(
max([data])
for [IndexNum] in ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10] )
)as Pvt2

这是来自表格的原始数据:

Conveyor_ID Alarm_Message   Alarm_datetime
0           PLC Loss Of Comm                                                2008-08-04 14:14:40.000
0           PLC Loss Of Comm                                                2008-08-04 13:55:54.000
0           PLC Loss Of Comm                                                2008-08-04 13:52:25.000
0           Oven Temp High Alarm                                            2008-08-04 13:10:20.000
0           E-stop Active                                                   2008-08-04 10:52:05.000
0           Oven Temp Low Alarm                                             2008-08-01 19:38:37.000
0           E-stop Active                                                   2008-08-01 10:19:01.000                                         2008-07-30 15:12:53.000

以下是未添加索引字段的数据

Conveyor_ID Data                   IndexNum
0           Aug  4 2008  2:14PM    1
0           PLC Loss Of Comm       2
0           Aug  4 2008  1:55PM    3
0           PLC Loss Of Comm       4
0           Aug  4 2008  1:52PM    5
0           PLC Loss Of Comm       6
0           Aug  4 2008  1:10PM    7
0           Oven Temp High Alarm   8
0           Aug  4 2008 10:52AM    9
0           E-stop Active          10
0           Aug  1 2008  7:38PM    11

最后,结果如下:

Conveyor_ID Alarm_1_1           Alarm_1_2                 Alarm_2_1             Alarm_2_2               Alarm_3_1            Alarm_3_2                  Alarm_4_1             Alarm_4_2                Alarm_5_1            Alarm_5_2
0           Aug  4 2008  2:14PM PLC Loss Of Comm          Aug  4 2008  1:55PM   PLC Loss Of Comm        Aug  4 2008  1:52PM  PLC Loss Of Comm           Aug  4 2008  1:10PM   Oven Temp High Alarm     Aug  4 2008 10:52AM  E-stop Active                                               
1           Jan  3 2009  6:58AM Low Bearing Air Fault     NULL                  NULL                    NULL                 NULL                       NULL                  NULL                     NULL                 NULL

再次,我要感谢Twelfth的快速反应以及他帮助我找到解决方案的第一部分。我没有试图以任何方式优化这一点,任何建议或改进都非常受欢迎。

文件。