使用查询对条件进行分组和检查

时间:2015-06-04 21:30:43

标签: sql sql-server sql-server-2008-r2

我被要求为以下情况编译SQL查询:

ID | PARAMETER  | VALID | VALUE
------------------------------
1  | Event1_Val | 1     | 0
1  | Event2_Val | 0     | 50.00
1  | Event2_Avg | 0     | 55.00
1  | Event1_Avg | 0     | 66.67

2  | Event1_Val | 0     | 37.50
2  | Event2_Val | 1     | 0
2  | Event1_Avg | 0     | 50.00
2  | Event2_Avg | 0     | 66.67

其中_Val是当前值。 _Avg是过去五个值的平均值。

要求1:如果VALID == 1(仅适用于_Val值),请勿检查该参数的_Val_Avg,但请检查另一个参数是_Val_Avg。在上面针对ID=1的示例中,将检查Event2_ValEvent2_Avg,因为Event1_Val.VALID = 1(这意味着Event1_ValEvent1_Avg将不会被选中。 )

要求2 :针对以下条件,检查要求1的结果_Val_Avg的值:

  • _Val< 5 OR
  • _Val + 10< _Avg

上述条件会返回警报。在上面针对ID=2的示例中,Event1_Val将返回警报,因为37.50 + 10小于50.00。

要求3:表明要求2的结果

ID | PARAMETER  | ALERT
------------------------
1  | Event2_Val | 0 
2  | Event1_Val | 1

我试图检查VALID是否不等于1,但是这给我留下了以下内容:

ID | PARAMETER  | VALID | VALUE
------------------------------
1  | Event2_Val | 0     | 50.00
1  | Event2_Avg | 0     | 55.00
1  | Event1_Avg | 0     | 66.67

2  | Event1_Val | 0     | 37.50
2  | Event1_Avg | 0     | 50.00
2  | Event2_Avg | 0     | 66.67

我不想检查_Val.VALID == 1事件的参数。含义,对于ID=1,我不想包含Event1_Avg

有没有办法在这种情况下使用查询对条件进行分组和检查?注意:强烈希望不更改表格中的值。

2 个答案:

答案 0 :(得分:1)

尝试:

SELECT t1.id,
   t1.parameter,
   CASE 
      WHEN (t1.value < 5 OR (t1.value + 10 < t2.value)) THEN 1
      ELSE 0
   END alert
FROM your_table t1
JOIN your_table t2 ON t1.id = t2.id
   AND left(t1.parameter, 6) = left(t2.parameter, 6)
   AND t1.parameter LIKE '%_Val'
   AND t2.parameter LIKE '%_Avg'
WHERE 
   t1.valid <> 1

答案 1 :(得分:0)

在这里,一些评论解释了最新进展:

declare @events table
(
ID int
,PARAMETER nvarchar(25)
,VALID bit
,VALUE decimal (18,2)
)

insert into @events(ID, PARAMETER,VALID, VALUE)

select 1  , 'Event1_Val' , 1     , 0
union all
select 1  , 'Event2_Val' , 0     , 50.00
union all
select 1  , 'Event2_Avg' , 0     , 55.00
union all
select 1  , 'Event1_Avg' , 0     , 66.67
union all
select 2  , 'Event1_Val' , 0     , 37.50
union all
select 2  , 'Event2_Val' , 1     , 0
union all
select 2  , 'Event1_Avg' , 0     , 50.00
union all
select 2  , 'Event2_Avg' , 0     , 66.67


select * from @events e

SELECT 
     ID
    ,PARAMETER 
    ,CASE WHEN (VALUE < 5 OR VALUE + 10.00 < AVRG) THEN 1 ELSE 0 END AS ALERT
FROM
(
    select 
        e.ID,
        MAX(CASE WHEN RIGHT(e.PARAMETER,4) = '_Val' THEN e.PARAMETER END ) as PARAMETER,
        MAX(CASE WHEN RIGHT(e.PARAMETER,4) = '_Val' THEN e.VALUE END) as VALUE,
        MAX(CASE WHEN RIGHT(e.PARAMETER,4) = '_Avg' THEN e.VALUE END) as AVRG
    from @events e
    JOIN
    ( --Get Valid Events
        select ID, REPLACE(REPLACE(PARAMETER,'_Val',''),'_Avg','') as Parameter --returns EventNAme without '_Val' or '_Avg' for grouping porpuses
        from @events
        WHERE RIGHT(PARAMETER,4) = '_Val' and VALID = 0
        GROUP BY ID ,REPLACE(REPLACE(PARAMETER,'_Val',''),'_Avg','')
    ) validEvents --filter by valid events only
        on validEvents.ID = e.ID
        AND validEvents.Parameter = REPLACE(REPLACE(e.PARAMETER,'_Val',''),'_Avg','')
    GROUP BY e.ID
) PIVOTEDRESULTS