我有一个包含发货的表(SQL Server 2008),因此“阶段 0”表示“新发货”,连续阶段是发货跟踪的不同阶段。
我正在尝试旋转下表:
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”是 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
变成类似的东西:
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
有什么帮助吗?谢谢