简体   繁体   English

SQL Server 2008中的数据透视表

[英]Pivot table in SQL-Server 2008

I have a table (SQL Server 2008) with shipments, so "Stage 0" means "New shipment" and consecutive stages are diferent stages of shipment tracking. 我有一个带有装运的表(SQL Server 2008),因此“阶段0”表示“新装运”,连续阶段是装运跟踪的不同阶段。

I'm trying to pivot the following table: 我正在尝试透视下表:

CREATE TABLE TableName
    ([ID] int, [IDShip] int, [Date] datetime, [Stage] int, [Arg] int)
;

INSERT INTO TableName
    ([ID], [IDShip], [Date], [Stage], [Arg])
VALUES
    (1, 1, '2013-10-03 08:36:00', 0, Null),
    (2, 1, '2013-10-03 08:37:25', 1, 1),
    (3, 2, '2013-10-03 08:38:25', 0, Null),
    (4, 1, '2013-10-03 08:39:25', 2, 1),
    (5, 2, '2013-10-03 08:40:25', 1, 3)
;

("Arg" is ID of Stage0. Select * would be:) (“ Arg”是Stage0的ID。选择*为:)

ID IDShip Date                Stage   Arg
1  1      2013-10-03 08:36:00     0  Null
2  1      2013-10-03 08:37:25     1     1
3  2      2013-10-03 08:38:25     0  Null
4  1      2013-10-03 08:39:25     2     1
5  2      2013-10-03 08:40:25     1     3

into something like: 变成类似:

ID0 IDShip DateShipment         ID1 DateFirstStage      ID2 DateSecondStage
1   1      2013-10-03 08:36:00  2   2013-10-03 08:37:25 4   2013-10-03 08:39:25    
3   2      2013-10-03 08:38:25  5   2013-10-03 08:40:25 

Any help? 有什么帮助吗? Thanks 谢谢

Turned out to be a bit more messy than I hoped for but here it is: 原来比我希望的更混乱,但这是:

SELECT MAX([0]) AS ID0,
    IDShip,
    (SELECT [Date] FROM TableName T1 WHERE T1.ID = MAX([0]) AND T1.IDShip = Y.IDShip) AS DateShipment,
    MAX([1]) AS ID1,
    (SELECT [Date] FROM TableName T2 WHERE T2.ID = MAX([1]) AND T2.IDShip = Y.IDShip) AS DateFirstStage,
    MAX([2]) AS ID2,
    (SELECT [Date] FROM TableName T3 WHERE T3.ID = MAX([2]) AND T3.IDShip = Y.IDShip) AS DateSecondStage
FROM
    (SELECT * FROM TableName
    PIVOT (MAX([ID]) FOR Stage IN ([0], [1], [2])) AS X) Y
GROUP BY IDShip

You first pivot the table into ID's of 3 stages and then select each stage and its date. 您首先将表旋转到3个阶段的ID,然后选择每个阶段及其日期。

It appears you need to pivot more than one column at the same time. 看来您需要同时旋转多个栏。 However, standard PIVOT syntax does not support multi-columnar pivoting. 但是,标准的PIVOT语法不支持多列透视。 You could use it to pivot one of the columns, then, using those as look-up values, pull the other column values with a series of correlated subqueries. 您可以使用它来旋转其中一列,然后将它们用作查找值,并通过一系列相关的子查询提取其他列的值。

My take on the approach was similar to @Szymon's, except I managed to avoid grouping in the outer query, although I made things more messy at other stages. 我采取的方法类似于@Szymon的方法,除了我设法避免在外部查询中进行分组,尽管在其他阶段使事情变得更加混乱。 Here is my attempt: 这是我的尝试:

SELECT
  IDShip,
  ID0,
  ID1,
  ID2,
  DateShipment    = (SELECT Date FROM TableName WHERE ID = p.ID0),
  DateFirstStage  = (SELECT Date FROM TableName WHERE ID = p.ID1),
  DateSecondStage = (SELECT Date FROM TableName WHERE ID = p.ID2)
FROM (
  SELECT
    ID,
    IDShip,
    StageID = 'ID' + CAST(Stage AS varchar(10))
  FROM TableName
) AS s
PIVOT (
  MAX(ID) FOR StageID IN (ID0, ID1, ID2)
) AS p
;

With proper indexing, it may not be too bad, although you could also try this alternative, which uses the older pivoting technique of grouping with conditional aggregation: 使用正确的索引,它可能还不错,尽管您也可以尝试以下替代方法,该方法使用了较早的数据透视分组技术和条件聚合:

SELECT
  IDShip,
  ID0 = MAX(CASE Stage WHEN 0 THEN ID END),
  ID1 = MAX(CASE Stage WHEN 1 THEN ID END),
  ID2 = MAX(CASE Stage WHEN 2 THEN ID END),
  DateShipment    = MAX(CASE Stage WHEN 0 THEN Date END),
  DateFirstStage  = MAX(CASE Stage WHEN 1 THEN Date END),
  DateSecondStage = MAX(CASE Stage WHEN 2 THEN Date END)
FROM TableName
GROUP BY IDShip
;

This SQL Fiddle demo will let you try both solutions. 此SQL Fiddle演示将让您尝试两种解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM