不要在ID上组合行

时间:2016-10-31 14:25:43

标签: sql sql-server pivot

我试图转动的表格如下:

|     Timestamp    | Userid | Event | message   | Id |
------------------------------------------------------
| 2016-09-01 08:40 | 600    | 0     | Created   | 1  |
| 2016-09-01 08:41 | 600    | 1     | Started   | 1  |
| 2016-09-01 08:42 | 600    | 2     | Completed | 1  |
| 2016-09-01 10:01 | 601    | 0     | Created   | 2  |
| 2016-09-01 10:01 | 601    | 1     | Started   | 2  |

这是我用来试图转动表格的代码。

with mytable as (
select TimeStamp
    ,UserId
    ,event
    ,message
    ,Id
from dbname.tablename
where TimeStamp between '2016-09-01' and '2016-10-01'
)
select Id
    ,[0] as created
    ,[1] as started
    ,[2] as completed
    ,[4] as mismatch_reported
    ,[5] as reqstd_qty_updated
    ,[6] as starting_update
    ,[7] as replaced
from mytable
pivot
(max(timestamp) for event in ([0],[1],[2],[4],[5],[6],[7])) as pivottable 

我的预期结果是:

| Id |      created     |      started     |    completed    | mismatch_reported | ... | replaced |
---------------------------------------------------------------------------------------------------
| 1  | 2016-09-01 08:40 | 2016-09-01 08:41 | 2016-09-01 8:42 | NULL              | ... | NULL     |
| 2  | 2016-09-01 10:01 | 2016-09-01 10:01 | NULL            | NULL              | ... | NULL     |

我得到的结果是将所有时间戳分成带有ID的各自的行,如下所示:

| Id |      created     |      started     |    completed    | mismatch_reported | ... | replaced |
---------------------------------------------------------------------------------------------------
| 1  | 2016-09-01 08:40 | NULL             | NULL            | NULL              | ... | NULL     |
| 1  | NULL             | 2016-09-01 08:41 | NULL            | NULL              | ... | NULL     |
| 1  | NULL             | NULL             | 2016-09-01 8:42 | NULL              | ... | NULL     |
| 2  | 2016-09-01 10:01 | NULL             | NULL            | NULL              | ... | NULL     |
| 2  | NULL             | 2016-09-01 10:01 | NULL            | NULL              | ... | NULL     |

我不知道为什么会这样做。在旋转方面,我是一个新手,但我看到的所有示例都显示了pivot命令的工作方式,正如我希望的那样。

此查询中的pivot语句出了什么问题?

______________________________________________________________

感谢TT。把我链接到这个! - 这将生成一个示例表供您进行故障排除

CREATE TABLE mytable(
   TimeStamp VARCHAR(23) NOT NULL PRIMARY KEY
  ,UserId    INTEGER  NOT NULL
  ,event     INTEGER  NOT NULL
  ,message   VARCHAR(31) NOT NULL
  ,Id        INTEGER  NOT NULL
);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:40:11.343',187,0,'Created',5);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:40:30.297',187,1,'Started',5);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:40:30.560',187,2,'Completed',5);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:42:00.167',238,1,'Started',6);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:41:59.667',238,2,'Completed',6);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:41:55.670',238,0,'Created',6);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:42:09.297',238,0,'Created',7);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:42:11.403',238,2,'Completed',7);
INSERT INTO mytable(TimeStamp,UserId,event,message,Id) VALUES ('2016-09-01 04:42:11.843',238,1,'Started',7);

1 个答案:

答案 0 :(得分:3)

我认为你在实际的表中有一些你想要进行数据透视的附加列,如果是这样的话,请尝试这样: 创建派生表(SourceTable),以过滤所需的列。

declare @a table ([Timestamp] datetime, [Event] int, Id int, a int )
insert into @a values ( '2016-09-01 08:40' , 0   ,   1 , 1 )
insert into @a values (  '2016-09-01 08:41',  1 ,     1 , 2 )
insert into @a values (  '2016-09-01 08:42',  2 ,     1 , 3  )
insert into @a values (  '2016-09-01 10:01',  0  ,    2  , 4 )
insert into @a values (  '2016-09-01 10:01' , 1  ,    2  , 5 )

 select Id
    ,[0] as created
    ,[1] as started
    ,[2] as completed
from
( 
select [Timestamp], [Event], Id from @a
) as SourceTable
pivot
(max(timestamp) for event in ([0],[1],[2])) as pivottable;

@ a-您的实际表和SourceTable派生表