【问题标题】:Find string and datetime difference查找字符串和日期时间差异
【发布时间】:2018-10-01 08:37:54
【问题描述】:

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

  • 进程 1 - 点击加载批次

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

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

  • 为批次 KLMHA3760600N31ETTM002SP2REB1 插槽 1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19 选择了流程 4-运行过程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 |
+-----------------------+----------------------------------------------------------------------------------------------------------------+------+------+

【问题讨论】:

  • 数据图片没有帮助;如果您要提供数据,请将其提供为text。此外,在发布问题时,实际提出问题非常重要(看起来很明显,对吧???????);你还没有在这里你刚刚陈述了你的目标。你的问题是什么?你试过什么了?您遇到了什么问题/错误?
  • 点击任意单元格,按CTRL+A,按CTRL+SHIFT+C,粘贴到文本文件中,整理一下。将整理好的数据粘贴到这里。不要发布屏幕截图。

标签: sql-server tsql


【解决方案1】:

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

这是一个查询(基于您的示例数据)来执行此操作。丑陋随你喜欢,但大多数情况下都能完成工作。

针对您的测试数据运行此查询的结果如下。我不确定如何解释第 3 阶段重复的“键”,也不知道如何解释您的“选择的过程”行不包括设备类型这一事实,您说这是一个关键属性。

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

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

-- 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

结果:

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,

【讨论】:

  • 嗨,Brett,我尝试在 sql store proc 上运行您的代码,但它得到了我,错误“关键字 'like' 附近的语法不正确。”。我是 sql server 的新手,但我发现 iif 语句必须在括号中包含 3 个参数不是吗?
  • 我已将脚本复制回我的环境,它按预期工作。您使用的是哪个版本的 SQL Server?我会为你发布一个 SQL Fiddle 链接..
  • 我用的是sql server 17
  • 您在第一条评论中说您将其作为存储过程运行。您是否确认脚本“按原样”工作而没有更改(除了我在第一个 cte 表达式中假设的源表名称/列)?如果是这样,那么听起来您在将其包装为存储过程时引入了一个错误。
【解决方案2】:

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

LEAD ( 表达式 [, 偏移量 [, 默认] ] ) OVER([query_partition_clause] order_by_clause)

【讨论】:

  • 这基本上是来自LEAD (Transact-SQL) 文档的复制/粘贴。发布时,最好引用您的信息,但也要为 OP 提供可用的示例,通常针对他们的需求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
相关资源
最近更新 更多