【问题标题】:Match data of two rows with different timestamp, into one row (SQL Server)将具有不同时间戳的两行数据匹配为一行(SQL Server)
【发布时间】:2018-09-07 06:32:33
【问题描述】:

我遇到了一个 SQL Server 查询问题。

您可以在下面的屏幕截图中看到我数据库中的数据示例。包含大数值属性的行实际上是读卡器读取的票的事件。

还有一些行包含像Eticket result 这样的字符串作为属性。当票证无效且系统显示票证无效的原因时产生该事件。

我想要做的是创建一个新列(命名为“原因”),其中将包含所有类似于“Eticket Result”的属性。问题是所有这些包含“ETICKET Result”的属性必须与正确的票号相匹配。原因属性与无效工单时间戳的时间差,永远不会超过500毫秒。

为了更容易理解,我在下面给你另一个屏幕截图,说明我想要做什么。

这可能吗?我已经尝试了几个小时并创建了无法生成正确结果的脚本。如果它对你有帮助,我会在我所做的查询下方给你,但没有显示正确的数据。

DECLARE @alarm_table TABLE (
 /*1*/server_timestamp     DATETIME ,
 /*2*/museum                 VARCHAR(255),
 /*3*/turnstile          VARCHAR(255),
 /*4*/entrance            VARCHAR(255),
 /*5*/cardnumber          VARCHAR(255),
 /*6*/result              VARCHAR(255),
 /*7*/reason              VARCHAR(255),
 /*8*/attributes             VARCHAR(255)

);

INSERT INTO @alarm_table
SELECT
  /*1*/servertimestamp,
  /*2*/hostname,
  /*3*/substring([hostname], PatIndex('%[0-9]%', [hostname]), len([hostname])),
  /*4*/substring(attributes, 31, 39),
  /*5*/attributes,
  /*6*/'NOT OK',
  /*7*/'',
  /*8*/attributes--substring(attributes, 70, 30)
FROM
  eventlog el
where
 (el.servertimestamp  BETWEEN '8/24/2018' AND DATEADD(dd, +1, '8/27/2019'))
 and (attributes like '%ticket %' and attributes like '%eticketing%' )
 and hostname <> 'tapaeos'
order by
  el.timestamp

 UPDATE @alarm_table
  SET cardnumber = substring(attributes, 31, 39)


  UPDATE @alarm_table
  SET result =  case
                   when 
                        (attributes like '%ticket 8%'
                        or attributes like '%ticket 9%'
                        or attributes like '%ticket 10%'
                        or attributes like '%ticket 11%' 
                        or attributes like '%ticket 12%' 
                        or attributes like '%ticket 13%' 
                        or attributes like '%ticket 14%' 
                        or attributes like '%knossos ticket 5%'
                        or attributes like '%knossos ticket 6%'
                        or attributes like '%knossos ticket 7%'
                        or attributes like '%klitys ticket 5%'
                        or attributes like '%klitys ticket 6%'
                        or attributes like '%klitys ticket 7%'
                        or attributes like '%olympieio ticket 5%'
                        or attributes like '%olympieio ticket 6%'
                        or attributes like '%olympieio ticket 7%'
                        ) 
                    then 'NOT OK'
                   else 'OK'
              end

  UPDATE @alarm_table
    SET reason = case
                    when result = 'NOT OK' then 
                    (SELECT top 1 attributes 
                     FROM eventlog 
                     WHERE DATEDIFF(second,servertimestamp,server_timestamp)<=1)
                   else ' '
              end

  UPDATE @alarm_table
    SET museum  = case
                 when museum like '%olymp%' then 'Olympieio'
                 when museum like '%knoss%' then 'Knossos'
                 when museum like '%sslope%' then 'Klitys'
                 when museum like '%acrop%' then 'Acropolis'
                end

  select
  server_timestamp,
  museum,
  turnstile,
  cardnumber,
  result,
  reason
 -- attributes
from
  @alarm_table



  order by server_timestamp desc  

非常感谢您的帮助,感谢您的宝贵时间。

【问题讨论】:

  • 请以文本的形式向我们展示样本数据的最小示例,而不是图像。您的图像数据很难阅读,我认为大多数人无法仅凭此回答。

标签: sql sql-server database sql-server-2017


【解决方案1】:

试试这个:

select e1.*, e2.attributes reason
from (
    select *
    from eventlog
    where charindex('ETICKET Result', attributes) = 0
) e1 left join (
    select timestamp, attributes
    from eventlog
    where charindex('ETICKET Result', attributes) > 0
) e2 on abs(datediff(millisecond, e1.timestamp, e2.timestamp)) <= 500

e1 我们查询所有正确的记录(没有ETICKET Reason),在e2 我们选择所有不正确的记录。然后我们以毫秒为单位加入两个结果的时间差。

【讨论】:

  • 感谢您的回复,我现在正在运行查询,如果有效,我会通知您
  • @we_mor 如果答案对您有用,请接受答案(左侧的绿色复选标记)并可选择投票。
  • 您的回答很有帮助。它没有得到正确的结果,但它帮助我将查询重建为一个有效的新查询。感谢您的帮助,我会将其标记为已接受。
猜你喜欢
  • 2020-12-05
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 2022-09-28
  • 2018-03-21
  • 2018-10-09
相关资源
最近更新 更多