将值转换为列名

时间:2021-07-22 13:23:39

标签: sql sql-server ssms

我应该写什么查询来传输这两个表:

表 1 - 事件日志

ID          Timestamp                Type       EventGroupID
10920061    2021-03-22 10:58:44.527  202        3F23B45E-052C-463D-BA07-A6DEC3D1356E
10920062    2021-03-22 10:58:45.207  202        C5ACC8F3-7B73-4090-ADF5-CAA7D0284AFA
10920063    2021-03-22 10:58:49.440  202        CA5AE246-8D14-41F0-B3C8-382B43EE2F52

表 2 - 事件日志数据

EventID     K           V
10920061    action      LiveFeedStart
10920061    cameraId    513
10920061    tracker     True
10920061    username    xxx
10920062    action      LiveFeedStart
10920062    cameraId    90
10920062    tracker     True
10920062    username    xxx
10920063    action      LiveFeedStart
10920063    cameraId    200
10920063    tracker     True
10920063    username    xxx

变成这样?

EventID   TimeStamp                Type   Action         Tracker  CameraId  Username
10920061  2021-03-22 10:58:44.527  202    LiveFeedStart  True     513       xxx
10920062  2021-03-22 10:58:45.207  202    LiveFeedStart  True     90        xxx

我只能制作这个

EventID     Timestamp                   Type    K   V
12943429    2021-07-12 10:57:45.4433333 203 action  LiveFeedStop
12943429    2021-07-12 10:57:45.4433333 203 bytesSent   95440687
12943429    2021-07-12 10:57:45.4433333 203 cameraId    914
12943429    2021-07-12 10:57:45.4433333 203 username    xxx
12943430    2021-07-12 10:57:51.2800000 203 action  LiveFeedStop

2 个答案:

答案 0 :(得分:3)

select * from EventLog el
left join (
select eventId
       ,max(case when K = 'action' then V end) action 
       ,max(case when K = 'bytesSent' then V end) bytesSent 
       ,max(case when K = 'cameraId' then V end) cameraId 
       ,max(case when K = 'username' then V end) username 
from EventLogData
group by eventId 
) eld
on el.Id = eld=eventid

答案 1 :(得分:1)

您可以使用 PIVOT 函数来获取结果。将您的数据放入几个表中进行测试:

CREATE TABLE EventLog
    ([ID] int, [Timestamp] datetime, [Type] int, [EventGroupID] varchar(36))
;
    
INSERT INTO EventLog
    ([ID], [Timestamp], [Type], [EventGroupID])
VALUES
    (10920061, '2021-03-22 10:58:44', 202, '3F23B45E-052C-463D-BA07-A6DEC3D1356E'),
    (10920062, '2021-03-22 10:58:45', 202, 'C5ACC8F3-7B73-4090-ADF5-CAA7D0284AFA'),
    (10920063, '2021-03-22 10:58:49', 202, 'CA5AE246-8D14-41F0-B3C8-382B43EE2F52')
;


CREATE TABLE EventLogData
    ([EventID] int, [K] varchar(8), [V] varchar(13))
;
    
INSERT INTO EventLogData
    ([EventID], [K], [V])
VALUES
    (10920061, 'action', 'LiveFeedStart'),
    (10920061, 'cameraId', '513'),
    (10920061, 'tracker', 'True'),
    (10920061, 'username', 'xxx'),
    (10920062, 'action', 'LiveFeedStart'),
    (10920062, 'cameraId', '90'),
    (10920062, 'tracker', 'True'),
    (10920062, 'username', 'xxx'),
    (10920063, 'action', 'LiveFeedStart'),
    (10920063, 'cameraId', '200'),
    (10920063, 'tracker', 'True'),
    (10920063, 'username', 'xxx')
;

然后使用 PIVOT 函数将行值转换为列名:

select *
from
(
  select 
    el.Id,
    el.[Timestamp],
    el.Type,
    eld.K,
    eld.V
  from EventLog el
  inner join EventLogData eld
    on el.Id = eld.EventId
) d
pivot
(
  max(V)
  for K in (Action, Tracker, CameraId, Username)
) piv

给出您正在寻找的结果。 (见demo