从Log表中提取信息并将其放入

时间:2017-07-13 17:45:32

标签: sql tsql

我有一个名为“Log”的下表。

LogID Description  Operator   LogType LogTimeStamp         JobNo  DNo
===== ============ ========   ======= ============         =====  ===
1     Job booked.. John       101     2016-01-01 00:00:00  44111  Null
2     Job booked.. Smith      101     2016-01-01 00:01:00  44112  Null
3     Offered...   (System)   104     2016-01-01 00:01:30  44111  230
4     Rejected...  (System)   105     2016-01-01 00:01:30  44111  230
5     Offered...   (System)   104     2016-01-01 00:02:00  44112  135
6     Offered...   (System)   104     2016-01-01 00:02:36  44111  042
7     Accepted...  (System)   107     2016-01-01 00:02:42  44111  042
8     Accepted...  (System)   107     2016-01-01 00:02:43  44112  135
9     Arrived...   (System)   108     2016-01-01 00:05:30  44111  042
10    POB...       (System)   109     2016-01-01 00:07:00  44111  042
11    Arrived...   (System)   108     2016-01-01 00:08:30  44112  135
12    POB...       (System)   109     2016-01-01 00:08:36  44112  135
13    Complted..   (System)   112     2016-01-01 00:15:00  44111  042
14    Forced Coml. John       1120    2016-01-01 00:20:30  44112  135

注意:上面(在描述栏中)只是一个提取。完整值如下所示:

Job Booked (Pickup time 01:18 Wed 01/06/2016) from Caller 07599999999 calling 300
Job on queue N Gwich accepted by driver on queue WTnC
Arrived at Stop 1
POB at Stop 1
ATTENTION Event Dismissed, 44111 has Rejected a Job
Arrived at Stop 2
POB at Stop 2
Job completed (Approx Paid Mileage = 1.6, Dead Mileage = 1.4)
Job needs to be verified - Account requires all jobs be verified

问题:是否可以按以下格式(列中)提取数据:

JobNo  DNo  Booked_By  Booked_On Offered_At Accepted_At Arrived_At POB_At   
Completed_At

到目前为止我所做的是以下内容:

SELECT 
   [LogID]
  ,[LogTimeStamp]
  ,[LogType]
  ,[JobNo]
  ,[Operator]
  ,[Description]
  ,[TelephoneNumber]
   ,CASE WHEN l.LogType = 101 THEN l.LogTimeStamp END AS JobBookedOn
   ,CASE WHEN l.LogType = 101 THEN l.Operator END AS JobBookedBy
   ,CASE WHEN l.LogType = 107 THEN l.LogTimeStamp END AS JobAcceptedOn
   ,CASE WHEN l.LogType = 108 AND l.Description LIKE '%Arrived%Stop 1%' 
THEN l.LogTimeStamp END AS ArrivedAtStop1
   ,CASE WHEN l.LogType = 110 AND l.Description LIKE '%POB%Stop 1%' THEN 
l.LogTimeStamp END AS POBAtStop1
   ,CASE WHEN l.LogType = 112 THEN l.LogTimeStamp END AS JobCompletedOn
   ,CASE WHEN l.LogType =   7 THEN l.LogTimeStamp END AS 
JobForcedCompletedOn
   ,CASE WHEN l.LogType = 301 THEN l.LogTimeStamp END AS CallReceivedOn
FROM [DB_A].[dbo].[Log] AS l
WHERE 
  (LogType = 101 OR
   LogType = 107 OR
   LogType = 108 OR
   LogType = 110 OR
   LogType = 112 OR
   LogType = 7 OR
   LogType = 301)
   AND
   l.JobNo = 4753784
ORDER BY LogTimeStamp DESC

到目前为止我所取得的成绩如下图所示。请注意,这是截断的图像。

Achievement so far

只能使用值获取一行,或者在没有值的情况下使用null。

我试过调查PIVOT,但无法掌握如何做到这一点。另一种选择看起来像CURSOR ......任何建议???

提前致谢。

额外信息:下面显示了创建命令的创建命令:

CREATE TABLE [dbo].[Log](
    [LogID] [int] IDENTITY(1,1) NOT NULL,
    [LogTimeStamp] [datetime] NOT NULL,
    [LogType] [int] NULL,
    [JobNo] [int] NULL,
    [DrvName] [varchar](50) NULL,
    [Operator] [varchar](16) NULL,
    [Description] [varchar](255) NOT NULL,
    [TelephoneNo] [varchar](30) NULL
) ON [PRIMARY]

要播放的一些数据(CSV文件):

LogID,LogTimeStamp,LogType,JobNo,DrvName,OperatorName,Description,TelephoneNumber
93147710,2015-05-25 13:22:14,101,4347551,NULL,John,Job Booked (Pickup time 14:22 Mon 25/05/2015) from Caller 0208317**** calling 300,NULL
93147711,2015-05-25 13:22:14,103,4347551,NULL,System(Queue),Job added to queue WichTwnC with 5 Priority Points,NULL
93147712,2015-05-25 13:22:14,104,4347551,44,System(Queue),Job offered to driver on queue WichTwnC at position 1,NULL
93147713,2015-05-25 13:22:15,107,4347550,56,System(Queue),Job on queue Cntrl Thmsmd accepted by driver on queue Cntrl Thmsmd,NULL
93147714,2015-05-25 13:22:15,204,NULL,56,System(Queue),Vehicle Car56 booked off queue Cntrl Thmsmd at position 1,NULL
93147715,2015-05-25 13:22:17,112,4347542,37,System(Progress),"Job completed (Approx Paid Mileage = 1.6, Dead Mileage = 0.0)",NULL
93147716,2015-05-25 13:22:18,203,NULL,37,System(Queue),Vehicle Car37 booked onto queue W Thmsmd at position 1,NULL
93147717,2015-05-25 13:22:19,107,4347551,44,System(Queue),Job on queue WichTwnC accepted by driver on queue WichTwnC,NULL
93147718,2015-05-25 13:22:19,204,NULL,44,System(Queue),Vehicle Car44 booked off queue WichTwnC at position 1,NULL
93147726,2015-05-25 13:23:09,104,4347548,79,System(Queue),Job offered to driver on queue Cntrl Thmsmd at position 2,NULL
93147727,2015-05-25 13:23:09,NULL,4347548,70,System(Queue),Job not offered to top Driver on main Queue because attributes don't match,NULL
93147728,2015-05-25 13:23:09,201,NULL,78,System(Signon),"Driver Signed On, Vehicle Car78, PDA serial number PC1EV",NULL
93147729,2015-05-25 13:23:11,116,4347537,4,System(Progress),Job is Late Arriving at the Pickup. Limit 3 min after Pickup Time,NULL
93147730,2015-05-25 13:23:16,107,4347548,79,System(Queue),Job on queue Cntrl Thmsmd accepted by driver on queue Cntrl Thmsmd,NULL
93147731,2015-05-25 13:23:16,204,NULL,79,System(Queue),Vehicle Car79 booked off queue Cntrl Thmsmd at position 2,NULL
93147732,2015-05-25 13:23:22,NULL,4347535,100,System(Progress),Reported: Quotation please.,NULL
93147733,2015-05-25 13:23:44,201,NULL,78,System(Signon),"Driver Signed On, Vehicle Car78, PDA serial number PC1EV",NULL
93147734,2015-05-25 13:23:45,101,4347552,NULL,John,Job Booked (Pickup time 14:23 Mon 25/05/2015),NULL
93147735,2015-05-25 13:23:45,103,4347552,NULL,System(Queue),Job added to queue WichTwnC with 5 Priority Points,NULL
93147736,2015-05-25 13:23:45,104,4347552,66,System(Queue),Job offered to driver on queue WichTwnC at position 1,NULL
93147737,2015-05-25 13:23:48,107,4347552,66,System(Queue),Job on queue WichTwnC accepted by driver on queue WichTwnC,NULL
93147738,2015-05-25 13:23:48,204,NULL,66,System(Queue),Vehicle Car66 booked off queue WichTwnC at position 1,NULL
93147739,2015-05-25 13:23:51,108,4347547,12,System(Progress),Arrived at Stop 1,NULL
93147740,2015-05-25 13:23:54,110,4347550,56,System(Progress),POB at Stop 1,NULL
93147741,2015-05-25 13:23:58,110,4347547,12,System(Progress),POB at Stop 1,NULL
93147742,2015-05-25 13:24:00,5,4347552,NULL,John,Job Changed,NULL
93147743,2015-05-25 13:24:00,NULL,4347552,NULL,John,Changed Job Driver Notes,NULL
93147744,2015-05-25 13:24:00,NULL,4347552,NULL,System(Progress),Job updated,NULL
93147745,2015-05-25 13:24:01,301,NULL,NULL,TRAINEE2,Received Caller 07789****** calling 300 on line 2,0791112223
93147746,2015-05-25 13:24:13,108,4347537,4,System(Progress),Arrived at Stop 1,NULL
93147747,2015-05-25 13:24:13,119,4347537,4,System(Progress),Vehicle was late by 4 minutes,NULL
93147748,2015-05-25 13:24:13,306,4347537,NULL,System(Progress),Job arrived - Text Message sent to 0784******,0791113335
93147749,2015-05-25 13:24:21,110,4347537,4,System(Progress),POB at Stop 1,NULL
93147750,2015-05-25 13:24:22,108,4347552,66,System(Progress),Arrived at Stop 1,NULL
93147751,2015-05-25 13:24:24,108,4347548,79,System(Progress),Arrived at Stop 1,NULL
93147752,2015-05-25 13:24:24,306,4347548,NULL,System(Progress),Job arrived - Text Message sent to 07580*******,0783336666
93147753,2015-05-25 13:24:26,202,NULL,78,System(Signon),"Driver Signed Off, Vehicle Car78",NULL
93147754,2015-05-25 13:24:49,110,4347548,79,System(Progress),POB at Stop 1,NULL
93147755,2015-05-25 13:24:50,NULL,4347535,100,System(Progress),Reported: Please complete my job,NULL
93147756,2015-05-25 13:25:12,101,4347553,NULL,TRAINEE2,Job Booked (Pickup time 14:25 Mon 25/05/2015) from Caller 0799999999 calling 300,NULL
93147757,2015-05-25 13:25:12,204,NULL,14,System(Queue),Vehicle Car14 booked off queue DockYrd at position 1,NULL
93147758,2015-05-25 13:25:12,203,NULL,14,System(Queue),Vehicle Car14 booked onto queue WichFerry at position 1,NULL
93147759,2015-05-25 13:25:12,103,4347553,NULL,System(Queue),Job added to queue WichC+Slp with 50 Priority Points,NULL
93147760,2015-05-25 13:25:13,112,4347526,87,System(Progress),"Job completed (Approx Paid Mileage = 6.6, Dead Mileage = 0.0)",NULL
93147761,2015-05-25 13:25:14,110,4347552,66,System(Progress),POB at Stop 1,NULL
93147762,2015-05-25 13:25:15,101,4347554,NULL,John,Job Booked (Pickup time 14:25 Mon 25/05/2015),NULL
93147763,2015-05-25 13:25:15,205,4347549,124,System(Progress),Reported problem: No Show,NULL
93147764,2015-05-25 13:25:16,103,4347554,NULL,System(Queue),Job added to queue WichTwnC with 5 Priority Points,NULL
93147765,2015-05-25 13:25:16,104,4347554,84,System(Queue),Job offered to driver on queue WichTwnC at position 1,NULL
93147766,2015-05-25 13:25:16,203,NULL,87,System(Queue),Vehicle Car87 booked onto queue Erith at position 1,NULL
93147767,2015-05-25 13:25:19,204,NULL,14,System(Queue),Vehicle Car14 booked off queue WichFerry at position 1,NULL
93147768,2015-05-25 13:25:19,203,NULL,14,System(Queue),Vehicle Car14 booked onto queue WichTwnC at position 10,NULL
93147769,2015-05-25 13:25:20,204,NULL,37,System(Queue),Vehicle Car37 booked off queue W Thmsmd at position 1,NULL
93147770,2015-05-25 13:25:20,203,NULL,37,System(Queue),Vehicle Car37 booked onto queue Cntrl Thmsmd at position 3,NULL
93147771,2015-05-25 13:25:21,107,4347554,84,System(Queue),Job on queue WichTwnC accepted by driver on queue WichTwnC,NULL
93147772,2015-05-25 13:25:21,204,NULL,84,System(Queue),Vehicle Car84 booked off queue WichTwnC at position 1,NULL
93147773,2015-05-25 13:25:24,301,NULL,NULL,John,Received Caller 0794******** calling 300 on line 2,02081114444
93147774,2015-05-25 13:25:39,110,4347551,44,System(Progress),POB at Stop 1,NULL
93147775,2015-05-25 13:25:42,301,NULL,NULL,Julie,Received Caller 07837896457 calling 300 on line 3,02072225555
93147776,2015-05-25 13:25:45,101,4347555,NULL,John,Job Booked (Pickup time 14:25 Mon 25/05/2015) from Caller 079********* calling 300,NULL
93147777,2015-05-25 13:25:46,103,4347555,NULL,System(Queue),Job added to queue WHAL S HILL with 5 Priority Points,NULL
93147778,2015-05-25 13:25:48,104,4347555,13,System(Queue),Job on queue WHAL S HILL offered to driver on backup queue QE HOSP,NULL
93147779,2015-05-25 13:25:48,5,4347553,NULL,TRAINEE2,Job Changed,NULL
93147780,2015-05-25 13:25:48,NULL,4347553,NULL,TRAINEE2,Changed Job Driver Notes,NULL
93147781,2015-05-25 13:25:49,301,NULL,NULL,John,Received Caller 07580****** calling 300 on line 2,02076668888
93147782,2015-05-25 13:25:51,301,NULL,NULL,TRAINEE2,Received Caller 07837****** calling 203 on line 3,07865465445
93147783,2015-05-25 13:25:51,204,NULL,14,System(Queue),Vehicle Car14 booked off queue WichTwnC at position 9,NULL
93147784,2015-05-25 13:25:51,203,NULL,14,System(Queue),Vehicle Car14 booked onto queue WichFerry at position 1,NULL
93147787,2015-05-25 13:25:53,NULL,4347535,100,System(Progress),Reported: Please complete my job,NULL
93147788,2015-05-25 13:25:54,NULL,4347553,NULL,System(Queue),Job removed from queue WichC+Slp because it was modified,NULL
93147791,2015-05-25 13:25:55,103,4347553,NULL,System(Queue),Job added to queue WichC+Slp with 52 Priority Points,NULL
93147792,2015-05-25 13:26:05,204,NULL,14,System(Queue),Vehicle Car14 booked off queue WichFerry at position 1,NULL
93147793,2015-05-25 13:26:05,203,NULL,14,System(Queue),Vehicle Car14 booked onto queue Woolwich Low at position 1,NULL
93147794,2015-05-25 13:26:06,101,4347556,NULL,John,Job Booked (Pickup time 14:26 Mon 25/05/2015) from Caller 075******** calling 300,NULL
93147795,2015-05-25 13:26:07,103,4347556,NULL,System(Queue),Job added to queue Cntrl Thmsmd with 5 Priority Points,NULL
93147796,2015-05-25 13:26:07,104,4347556,70,System(Queue),Job offered to driver on queue Cntrl Thmsmd at position 1,NULL
93147797,2015-05-25 13:26:08,7,4347535,100,Julie,Job Forced Complete. Reason given: CC,NULL
93147798,2015-05-25 13:26:08,301,NULL,NULL,John,Received Caller 02083178728 calling 300 on line 2,02083178999
93147799,2015-05-25 13:26:09,107,4347556,70,System(Queue),Job on queue Cntrl Thmsmd accepted by driver on queue Cntrl Thmsmd,NULL
93147800,2015-05-25 13:26:09,204,NULL,70,System(Queue),Vehicle Car70 booked off queue Cntrl Thmsmd at position 1,NULL
93147801,2015-05-25 13:26:11,105,4347555,13,System(Queue),Job rejected by driver; Job remains on queue WHAL S HILL,NULL
93147802,2015-05-25 13:26:20,6,4347549,NULL,Julie,Operator contacted passenger by telephone,07507999486
93147803,2015-05-25 13:26:23,205,4347549,124,System(Progress),Reported problem: No Show,NULL
93147804,2015-05-25 13:26:26,108,4347554,84,System(Progress),Arrived at Stop 1,NULL
93147805,2015-05-25 13:26:29,101,4347557,NULL,John,Job Booked (Pickup time 14:26 Mon 25/05/2015) from Caller 0208317**** calling 300,NULL
93147806,2015-05-25 13:26:29,103,4347557,NULL,System(Queue),Job added to queue WichTwnC with 5 Priority Points,NULL
93147807,2015-05-25 13:26:29,104,4347557,36,System(Queue),Job offered to driver on queue WichTwnC at position 2,NULL
93147808,2015-05-25 13:26:29,NULL,4347557,22,System(Queue),Job not offered to top Driver on main Queue because the Account is restricted,NULL
93147809,2015-05-25 13:26:29,112,4347538,41,System(Progress),"Job completed (Approx Paid Mileage = 3.9, Dead Mileage = 0.0)",NULL
93147810,2015-05-25 13:26:30,203,NULL,41,System(Queue),Vehicle Car41 booked onto queue Up Belveder at position 1,NULL
93147811,2015-05-25 13:26:31,203,NULL,100,System(Queue),Vehicle Car100 booked onto queue Blackheath at position 1,NULL
93147812,2015-05-25 13:26:31,127,4347553,NULL,System(Queue),Job is high enough priority to override normal Queue processing,NULL
93147813,2015-05-25 13:26:37,NULL,4347547,NULL,TRAINEE2,"ATTENTION Event Dismissed, 36 has Rejected a Job",NULL
93147814,2015-05-25 13:26:37,NULL,4347547,NULL,TRAINEE2,"ATTENTION Event Dismissed, 66 has Rejected a Job",NULL
93147815,2015-05-25 13:26:37,NULL,4347548,NULL,TRAINEE2,"ATTENTION Event Dismissed, 79 has Rejected a Job",NULL
93147816,2015-05-25 13:26:37,NULL,4347547,NULL,TRAINEE2,"ATTENTION Event Dismissed, 44 has Rejected a Job",NULL

1 个答案:

答案 0 :(得分:1)

这就是你要做的事情:

enter image description here

请注意,由于未提供此字段,我的答案缺失DNo。但是,假设DNoJobNo的关系为1:1,那么您只需将其添加到SELECTGROUP BY即可。

如果是这样,那么你几乎就在那里:

 SELECT [JobNo],
           MAX(CASE
                   WHEN l.LogType = 101
                   THEN l.LogTimeStamp
               END) AS JobBookedOn,
           MAX(CASE
                   WHEN l.LogType = 101
                   THEN l.[OperatorName]
               END) AS JobBookedBy,
           MAX(CASE
                   WHEN l.LogType = 107
                   THEN l.LogTimeStamp
               END) AS JobAcceptedOn,
           MAX(CASE
                   WHEN l.LogType = 108
                        AND l.Description LIKE '%Arrived%Stop 1%'
                   THEN l.LogTimeStamp
               END) AS ArrivedAtStop1,
           MAX(CASE
                   WHEN l.LogType = 110
                        AND l.Description LIKE '%POB%Stop 1%'
                   THEN l.LogTimeStamp
               END) AS POBAtStop1,
           MAX(CASE
                   WHEN l.LogType = 112
                   THEN l.LogTimeStamp
               END) AS JobCompletedOn,
           MAX(CASE
                   WHEN l.LogType = 7
                   THEN l.LogTimeStamp
               END) AS JobForcedCompletedOn,
           MAX(CASE
                   WHEN l.LogType = 301
                   THEN l.LogTimeStamp
               END) AS CallReceivedOn
    FROM [dbo].[Log] AS l
    WHERE(LogType = 101
          OR LogType = 107
          OR LogType = 108
          OR LogType = 110
          OR LogType = 112
          OR LogType = 7
          OR LogType = 301)
         AND l.JobNo = 4347551
    GROUP BY [JobNo];

见上述结果。