用SQL查询确定状态?

时间:2008-09-23 16:23:55

标签: sql sql-server

我在MSSQL数据库中有一个表,如下所示:

Timestamp (datetime)
Message (varchar(20))

每天一次,特定流程会在启动时插入当前时间和“已启动”消息。完成后,它会插入当前时间和消息“完成”。

在特定日期返回时,什么是好的查询或一组语句:

    如果进程从未开始,则
  • 0
  • 1如果流程已开始但尚未完成
  • 2如果流程已开始并完成

表格中还有其他消息,但“已启动”和“已完成”对于此过程是唯一的。

编辑:对于奖励业力,如果数据无效则引发错误,例如有两条“已启动”消息,或者存在“完成”而没有“已启动”。

4 个答案:

答案 0 :(得分:2)

Select Count(Message) As Status
From   Process_monitor
Where  TimeStamp >= '20080923'
       And TimeStamp < '20080924'
       And (Message = 'Started' or Message = 'Finished')

您可以稍微修改一下,以检测无效条件,例如多次启动,完成,没有完成的启动等等。

Select  Case When SumStarted = 0 And SumFinished = 0 Then 'Not Started'
             When SumStarted = 1 And SumFinished = 0 Then 'Started'
             When SumStarted = 1 And SumFinished = 1 Then 'Finished'
             When SumStarted > 1 Then 'Multiple Starts' 
             When SumFinished > 1 Then 'Multiple Finish'
             When SumFinished > 0 And SumStarted = 0 Then 'Finish Without Start'
             End As StatusMessage
From    (
          Select Sum(Case When Message = 'Started' Then 1 Else 0 End) As SumStarted,
                 Sum(Case When Message = 'Finished' Then 1 Else 0 End) As SumFinished
          From   Process_monitor
          Where  TimeStamp >= '20080923'
                 And TimeStamp < '20080924'
                 And (Message = 'Started' or Message = 'Finished')
        ) As AliasName

答案 1 :(得分:0)

DECLARE @TargetDate datetime
SET @TargetDate = '2008-01-01'

DECLARE @Messages varchar(max)

SET @Messages = ''

SELECT @Messages = @Messages + '|' + Message
FROM process_monitor
WHERE @TargetDate <= Timestamp and Timestamp < DateAdd(dd, 1, @TargetDate)
   and Message in ('Finished', 'Started')
ORDER BY Timestamp desc

SELECT CASE
  WHEN @Messages = '|Finished|Started' THEN 2
  WHEN @Messages = '|Started' THEN 1
  WHEN @Messages = '' THEN 0
  ELSE -1
END

答案 2 :(得分:0)

您缺少唯一标识进程的列。让我们添加一个名为ProcessID的int列。您还需要另一个表来标识进程。如果您依赖原始表,那么您永远不会知道从未开始的进程,因为该进程没有任何行。

select
    ProcessID,
    ProcessName,

    CASE
    WHEN 
       (Select 
           COUNT(*) 
        from 
           ProcessActivity 
        where 
           ProcessActivity.processid = Processes.processid 
           and Message = 'STARTED') = 1 

        And
       (Select 
           COUNT(*) 
        from 
           ProcessActivity 
        where 
           ProcessActivity.processid = Processes.processid 
           and Message = 'FINISHED') = 0
     THEN 1

     WHEN
       (Select 
           COUNT(*) 
        from 
           ProcessActivity 
        where 
           ProcessActivity.processid = Processes.processid 
           and Message = 'STARTED') = 1 
       And
       (Select 
           COUNT(*) 
        from 
           ProcessActivity 
        where 
           ProcessActivity.processid = Processes.processid 
           and Message = 'FINISHED') = 1 
THEN 2
     ELSE 0

END as Status

From
    Processes

答案 3 :(得分:-1)

select count(*) from process_monitor 
where timestamp > yesterday and timestamp < tomorrow.

或者,您可以使用带有最大值的自我联接来显示特定日期的最新消息:

select * from process_monitor where 
timestamp=(select max(timestamp) where timestamp<next_day);