SQL对日志表中的记录进行分组

SQL对日志表中的记录进行分组,sql,sql-server,Sql,Sql Server,我有一个记录系统中每个事件的表,如“排队”、“开始”、“完成”、“失败”等。。。表中还有很多步骤,但这些都是我感兴趣的 我只从中选择我想要的事件 SELECT [Id] ,[EventTime] ,[Message] FROM [Log] WHERE [Message] LIKE '%Queued%' OR [Message] LIKE '%Started%' OR [Message] LIKE '%Finished%' OR [Message] LIKE '%Failed%' 这给了我类似

我有一个记录系统中每个事件的表,如“排队”、“开始”、“完成”、“失败”等。。。表中还有很多步骤,但这些都是我感兴趣的

我只从中选择我想要的事件

SELECT [Id]
,[EventTime]
,[Message]
FROM [Log] 
WHERE [Message] LIKE '%Queued%'
OR [Message] LIKE '%Started%'
OR [Message] LIKE '%Finished%'
OR [Message] LIKE '%Failed%'
这给了我类似的东西

Id      EventTime  Message
5764    2013-12-20 17:52:25.037 Queued
5764    2013-12-20 17:53:09.767 Started
5765    2013-12-20 17:55:50.403 Queued
5764    2013-12-20 17:57:07.503 Finished
5765    2013-12-20 17:57:39.010 Started
5765    2013-12-20 17:58:05.553 Failed
我希望从这个查询中得到以下格式的记录集

Id、排队时间、开始时间、完成时间、持续时间、状态

现在,如果只有QueuedTime,状态列应为“排队”;如果有开始但没有完成时间,状态列应为“InProgress”;如果有开始和完成时间,状态列应为“Success”;如果有失败时间,状态列应为“Failed”

我知道我需要某种状态列的case语句,但我不确定如何在同一行中使用一个Id的所有内容


有人能提供一些帮助来实现这一点吗?

您缺少一个变量,用于将所有这些日志分组到一个会话中

在您的查询中,它们可能是多个状态为“Started”的日志,因此您需要有一个“requestId”或类似的内容,每个类型包含一个日志。

尝试这样做

SELECT ID,MIN(EventTime),MAX(EventTime),TIMEDIFF(MAX(EventTime),MIN(EventTime)) * 24*60*60 AS DURATION,Message
FROM TABLE1
GROUP BY ID

可能是这样的:(对不起,我只知道Oracle SQL和时间格式可能是错误的,但这是我的基本想法:))


您可以通过多个左外部联接来实现这一点,但是请注意,如果您的数据库中有大量数据,那么性能可能会很差。从性能的角度来看,用LIKE查询消息列也不是一个好主意

您是否想过在将数据加载到数据库之前使用某种ETL工具来准备数据(假设数据来自外部源)

无论如何,以下是您可以做的:

select
    q.id,
    q.eventtime as QueuedTime,
    s.eventtime as StartTime,
    f.eventtime as FinishTime,
    f.eventtime-s.eventtime as Duration,
    case
        when fail.id is not null then 'Failed'
        when (q.eventtime is not null and s.eventtime is null and f.eventtime is null) then 'Queued'
        when (q.eventtime is not null and s.eventtime is not null and f.eventtime is null) then 'InProgress'
        when (q.eventtime is not null and s.eventtime is not null and f.eventtime is not null) then 'Success'
    end as Status
from
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Queued%'
    ) q
left outer join
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Started%'
    ) s
on
    q.id = s.id
left outer join
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Finished%'
    ) f
on
    q.id = f.id
left outer join
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Failed%'
    ) fail
on
    q.id = fail.id

Id列应该是这辆车的一部分。它更像是一个jobid,而不是一个行标识符,当。。。然后其他的是否使用子查询结束?
select
    q.id,
    q.eventtime as QueuedTime,
    s.eventtime as StartTime,
    f.eventtime as FinishTime,
    f.eventtime-s.eventtime as Duration,
    case
        when fail.id is not null then 'Failed'
        when (q.eventtime is not null and s.eventtime is null and f.eventtime is null) then 'Queued'
        when (q.eventtime is not null and s.eventtime is not null and f.eventtime is null) then 'InProgress'
        when (q.eventtime is not null and s.eventtime is not null and f.eventtime is not null) then 'Success'
    end as Status
from
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Queued%'
    ) q
left outer join
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Started%'
    ) s
on
    q.id = s.id
left outer join
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Finished%'
    ) f
on
    q.id = f.id
left outer join
    (
        select
            distinct id,
            eventtime
        from
            log
        where
            message like '%Failed%'
    ) fail
on
    q.id = fail.id