Sql server 查找字符串和日期时间差

Sql server 查找字符串和日期时间差,sql-server,tsql,Sql Server,Tsql,我需要找出从第一道工序到第二道工序,再从第二道工序到第三道工序需要多长时间。表中不同的是lotid、设备类型和插槽。我需要在表中找到多个字符串 流程1-单击加载批次 过程2-使用批次ID KLMHA3760600N31ETTM002SP2REB1设备类型Mamba-6HR-CC3E的“Q-check”按钮开始Q-check 过程3-Q检查使用批次ID KLMHA3760600N31ETTM002SP2REB1设备类型Mamba-6HR-CC3E的“启动”按钮启动 工艺4-为批次KLMHA376

我需要找出从第一道工序到第二道工序,再从第二道工序到第三道工序需要多长时间。表中不同的是lotid、设备类型和插槽。我需要在表中找到多个字符串

  • 流程1-单击加载批次

  • 过程2-使用批次ID KLMHA3760600N31ETTM002SP2REB1设备类型Mamba-6HR-CC3E的“Q-check”按钮开始Q-check

  • 过程3-Q检查使用批次ID KLMHA3760600N31ETTM002SP2REB1设备类型Mamba-6HR-CC3E的“启动”按钮启动

  • 工艺4-为批次KLMHA3760600N31ETTM002SP2REB1插槽1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21选择运行工艺

这就是我的数据库的外观:

+-----------------------+----------------------------------------------------------------------------------------------------------------+------+------+
|          Time         |                                                    Process                                                     | Oven | Line |
+-----------------------+----------------------------------------------------------------------------------------------------------------+------+------+
| 10/1/2018 8:45:45 AM  | Load Lot Clicked                                                                                               |    2 | 4072 |
| 10/1/2018 8:47:22 AM  | Q-check Started using 'Q-Check' Button for Lot ID KLMHA3760600N31ETTM002SP2REB1 Device Type Mamba-6HR-CC3E     |    2 | 4073 |
| 10/1/2018 8:47:22 AM  | - slots = 1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,"                                                             |    2 | 4074 |
| 10/1/2018 8:47:34 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M5674_DRV_V53.s19, 0 retries with 0 of 16 needing reload"     |    2 | 4075 |
| 10/1/2018 8:47:37 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M567x_V053_1.s19, 0 retries with 0 of 16 needing reload"      |    2 | 4076 |
| 10/1/2018 8:51:29 AM  | Close Selected from BIB screen for lot                                                                         |    2 | 4077 |
| 10/1/2018 8:51:31 AM  | BIB Screen opened on slot 1                                                                                    |    2 | 4078 |
| 10/1/2018 9:02:04 AM  | Close Selected from BIB screen for lot                                                                         |    2 | 4079 |
| 10/1/2018 9:02:44 AM  | Q-check Started using 'Q-Check' Button for Lot ID KLMHA3760600N31ETTM002SP2REB1 Device Type Mamba-6HR-CC3E     |    2 | 4080 |
| 10/1/2018 9:02:44 AM  | - slots = 1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21,"                                                       |    2 | 4081 |
| 10/1/2018 9:02:57 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M5674_DRV_V53.s19, 0 retries with 0 of 18 needing reload"     |    2 | 4082 |
| 10/1/2018 9:03:00 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M567x_V053_1.s19, 0 retries with 0 of 18 needing reload"      |    2 | 4083 |
| 10/1/2018 9:07:01 AM  | Close Selected from BIB screen for lot                                                                         |    2 | 4084 |
| 10/1/2018 9:07:04 AM  | Q-check Started using 'Start' Button for Lot ID KLMHA3760600N31ETTM002SP2REB1 Device Type Mamba-6HR-CC3E       |    2 | 4085 |
| 10/1/2018 9:07:04 AM  | - slots = 1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21,"                                                       |    2 | 4086 |
| 10/1/2018 9:07:19 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M5674_DRV_V53.s19, 0 retries with 0 of 18 needing reload"     |    2 | 4087 |
| 10/1/2018 9:07:22 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M567x_V053_1.s19, 0 retries with 0 of 18 needing reload"      |    2 | 4088 |
| 10/1/2018 9:09:31 AM  | Cancel Proc Selected for lot KLMHA3760600N31ETTM002SP2REB1                                                     |    2 | 4089 |
| 10/1/2018 9:09:43 AM  | BIB Screen opened on slot 1                                                                                    |    2 | 4090 |
| 10/1/2018 9:09:55 AM  | Close Selected from BIB screen for lot                                                                         |    2 | 4091 |
| 10/1/2018 9:10:38 AM  | Q-check Started using 'Start' Button for Lot ID KLMHA3760600N31ETTM002SP2REB1 Device Type Mamba-6HR-CC3E       |    2 | 4092 |
| 10/1/2018 9:10:38 AM  | - slots = 1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21,"                                                       |    2 | 4093 |
| 10/1/2018 9:10:53 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M5674_DRV_V53.s19, 0 retries with 0 of 18 needing reload"     |    2 | 4094 |
| 10/1/2018 9:10:56 AM  | - Completed Loading C:\masters\winapps\MBI\diags\M567x_V053_1.s19, 0 retries with 0 of 18 needing reload"      |    2 | 4095 |
| 10/1/2018 9:12:58 AM  | - Run Proc Selected for lot KLMHA3760600N31ETTM002SP2REB1 slots 1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21," |    2 | 4096 |
+-----------------------+----------------------------------------------------------------------------------------------------------------+------+------+

使用LEAD分析函数。它返回表中下一行的值

前导(表达式[,偏移量[,默认值])
OVER([query\u partition\u clause]order\u by\u clause)

我认为本质上你想要清理你的数据并提取你的关键字段。然后你就可以计算你的指标了

这里有一个查询(基于您的示例数据)来完成这项工作。虽然它很难看,但基本上可以完成工作

对您的测试数据运行此查询的结果如下。我不确定如何解释第3阶段重复的“键”,也不确定如何解释“Proc Selected”行不包括设备类型这一事实,您认为该设备类型是键属性

因此,出于必要,我将任何改进和运行时间的计算留给你,@sapkoklu

注意:我假设
Q-check
行及其关联的
slot
行具有完全相同的时间戳,就像示例数据中的情况一样

-- the first CTE just pulls the rows of the log that you need to identify your steps.
;with cte ([Time], Process, Oven, [LineNo])
as
(
    select * from SourceDataTable
    where Process like '%Load Lot Clicked%'
       or Process like '%Q-check%'
       or Process like '%- slots%'
       or Process like '%Run Proc Selected%'
)
-- The second cte joins the first cte to itself, so we can get the slots along with the lotId and DeviceType.
-- Then we pull out the key fields to enable further analysis.
-- We also add the "step" to make life a bit easier for ourselves.
, cte2 ([Time], Process, Oven, [LineNo], LotId, DeviceType, Slots, Step)
as
(
    select c1.*
    , iif(c1.Process like '%Q-check%',           substring(c1.Process, charindex('Lot ID', c1.Process) + 7, charindex(' ', c1.Process, charindex('Lot ID', c1.Process) + 8) - charindex('Lot ID', c1.Process) - 7) ,
        iif(c1.Process like '%Run Proc Selected%', substring(c1.Process, charindex('for lot', c1.Process) + 8, charindex(' ', c1.Process, charindex('for lot', c1.Process) + 9) - charindex('for lot', c1.Process) - 8)                     
                , null))
    , iif(c1.Process like '%Q-check%', substring(c1.Process, charindex('Device Type', c1.Process) + 12, 255), null)
    , iif(c1.Process like '%Run Proc Selected%', SUBSTRING(c1.Process, charindex('slots ', c1.Process) +6, len(c1.Process) - charindex('slots ', c1.Process) - 6)
        , SUBSTRING(c2.Process, charindex('slots =', c2.Process) +8, len(c2.Process) - charindex('slots =', c2.Process) - 8))
    , case 
        when c1.Process like '%Load Lot Clicked%' then 1
        when c1.Process       like '%''Q-Check'' Button%' then 2
        when c1.Process   like '%''Start'' Button%' then 3
        when c1.Process    like '%Run Proc Selected%' then 4
      end
    from cte c1
    left join cte c2
           on c1.Oven = c2.Oven
          and c1.[Time] = c2.[Time]
          and c1.[LineNo] < c2.[LineNo]
    where c1.Process not like '%- slots%'
)
select [Time], Step, Oven, [LineNo], LotId, DeviceType, Slots from cte2

数据图片没有帮助;如果您要提供数据,请将其作为
文本提供。此外,在发布问题时,实际提问非常重要(说起来似乎很明显,对吧?点击任何单元格,按CTRL+A,按CTRL+SHIFT+C,然后将其粘贴到文本文件中并整理。将整理数据粘贴到此处。不要发布截图。这基本上是从的文档中复制/粘贴。发布时,最好引用您的信息,但也可以为OP提供可用的示例,norma您好,Brett,我试着在sql store proc上运行您的代码,结果发现,关键字“like”附近的错误语法。".我是sql server的新手,但我发现iif语句必须在括号中包含3个参数,不是吗?我已将脚本复制回我的环境中,并按预期工作。您使用的是哪个版本的sql server?我将为您发布一个sql Fiddle链接。.我使用sql server 17您在第一条评论中说,您将此作为存储过程运行。是吗确认脚本“按原样”工作而不进行更改(除了我在第一个cte表达式中假定的源表名称/列之外)?如果是,那么当您将其包装为存储过程时,听起来好像您引入了错误。
Time                    Step  Oven  LineNo  LotId                          DeviceType       Slots
----------------------- ----- ----- ------- ------------------------------ ---------------- ---------------------------------------------
2018-10-01 08:45:45.000 1     2     4072    NULL                           NULL             NULL
2018-10-01 08:47:22.000 2     2     4073    KLMHA3760600N31ETTM002SP2REB1  Mamba-6HR-CC3E   1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,
2018-10-01 09:02:44.000 2     2     4080    KLMHA3760600N31ETTM002SP2REB1  Mamba-6HR-CC3E   1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21,
2018-10-01 09:07:04.000 3     2     4085    KLMHA3760600N31ETTM002SP2REB1  Mamba-6HR-CC3E   1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21,
2018-10-01 09:10:38.000 3     2     4092    KLMHA3760600N31ETTM002SP2REB1  Mamba-6HR-CC3E   1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21,
2018-10-01 09:12:58.000 4     2     4096    KLMHA3760600N31ETTM002SP2REB1  NULL             1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19,21,