3

I have a two tables with alarming information from multiple different computers. I would like to return a list of specific alarms for a specific computer, and also include the previous alarm for that same computer.

Table 1: Computer Info

  ComputerInfoId| ComputerName| [Desc]
        1       |  Desktop1   | Front Desk
        2       |  Laptop1    | Work Station 1

Table 2: AlarmData

AlarmDataId|ComputerInfoId|AlarmId|AlarmDate
     1          2             1     03Mar2014 01:12:29
     2          2             3     03Mar2014 01:12:30
     3          1             7     03Mar2014 01:12:33
     4          1             2     03Mar2014 01:12:36
     5          1             6     03Mar2014 01:14:29
     6          2            12     03Mar2014 01:15:30
     7          1             2     03Mar2014 01:16:12
     8          2             1     03Mar2014 01:19:40

I would like a query for ComputerInfoID 1 and AlarmId 2. A query would return those records as well as the 2 records for that station that happened previous.

Expected Data:

ComputerInfoId|ComputerName|AlarmDataId|AlarmId|     AlarmDate    
      1       |  Desktop1  |    3      |  7    |03Mar2014 01:12:33 --Prev Record
      1       |  Desktop1  |    4      |  2    |03Mar2014 01:12:36 --Record fits Criteria
      1       |  Desktop1  |    5      |  6    |03Mar2014 01:14:29 --Prev Record
      1       |  Desktop1  |    7      |  2    |03Mar2014 01:16:12 --Record fist Criteria

The comments after each line are not required in the output data, they are included here for clarity.

The query that I have been working with is:

 SELECT * 
 FROM AlarmData AD LEFT OUTER JOIN 
        (SELECT *
         FROM   AlarmData AD2 INNER JOIN 
                 (SELECT  MAX(AlarmDatId)[MaxADID]
                  FROM    AlarmData AD3
                  WHERE   AD3.AlarmDataId < AD.AlarmDataId 
                         AND AD3.ComputerInfoID = AD.ComputerInfoID) AMax
           ON AD2.AlarmDataId = AMax.MaxADID) PrevAl
             ON PrevAl.ComputerInfoID = AD.ComputerInfoID 
 WHERE AD.ComputerInfoID = 1 AND AD.AlarmId = 2

But I am getting an error that the AD.AlarmDataId and AD.ComputerInfoID could not be bound on the "AD3.AlarmDataId < AD.AlarmDataId AND AD3.ComputerInfoID = AD.ComputerInfoID" line.

5
  • Do you need the last alarm for computerID =1 and alarmID = 2?
    – Javaluca
    Apr 16, 2015 at 19:03
  • The previous alarm for each instance of computerId = 1 and AlarmId=2
    – Steven
    Apr 16, 2015 at 19:11
  • 1
    Can you write the expected result for the data in example?
    – Javaluca
    Apr 16, 2015 at 19:20
  • The question has been edited to include the expected data.
    – Steven
    Apr 16, 2015 at 19:32
  • The question was updated to clearly depict that the previous record is not always [Record that fits criteria AlarmId) - 1
    – Steven
    Apr 16, 2015 at 20:00

5 Answers 5

1

Your sample output data doesn't match the input data,and I can't tell if you want two previous errors for each matching error, or one previous error for each of 2 errors in the sample data. I think that this query will work on a larger dataset where the previous alarm for a machine might not be the previous AlarmDataID.

I have indicated with a comment where to change the number of surrounding rows you select.

with Errors  as (
select *, ROW_NUMBER() over( order by alarmdate) as OrdCnt
from alarmdata AD
  WHERE AD.ComputerInfoID = 1
)
select c.ComputerName, f.*
from ComputerInfo c 
    left outer join errors e
        on c.computerInfoId = e.ComputerInfoID 
    left outer join errors f
        on f.OrdCnt between (e.OrdCnt - 1 )and (e.OrdCnt + 0) -- Number of rows before and after
where (e.AlarmID = 2)
2
  • The results are not a predefined number of entries before the desired row. The previous result is based upon having the same Computer Id.
    – Steven
    Apr 16, 2015 at 20:35
  • @Steven, sounds good. That's what I figured you probably wanted, so that's I wrote. You will see how I find that in the CTE. Just run the select statement from the "WITH" statement. Hope it helps. Apr 16, 2015 at 20:43
1

I think this work for you :

Select ComputerInfo.ComputerInfoId ,ComputerInfo.ComputerName
      ,AlarmData.AlarmDataId ,AlarmData.AlarmId ,AlarmData.AlarmDate
      ,TB.AlarmId As PreAlarmId ,TB.AlarmDate As PreAlarmDate

From ComputerInfo
inner join AlarmData On AlarmData.ComputerInfoId =ComputerInfo.ComputerInfoId
Left Outer join AlarmData TB 
                          On TB.ComputerInfoId = ComputerInfo.ComputerInfoId
                         And TB.AlarmDataId < AlarmData.AlarmDataId
                         And TB.AlarmDataId in ( Select Max(a_1.AlarmDataId)  From AlarmData As a_1
                                                        Where a_1.ComputerInfoId = ComputerInfo.ComputerInfoId
                                                        And a_1.AlarmDataId < AlarmData.AlarmDataId)
where ComputerInfo.ComputerInfoId = 1
And AlarmData.AlarmId = 2

this is sql fiddle link : Sql Fiddle Link

0

Your result is unclear (AlarmDataId = 5 is for ComputerInfoId = 2 NOT 1)

But I wrote something hope is what you need

SELECT a.ComputerInfoID, 
       (SELECT ComputerName 
         FROM ComputerInfo c 
         WHERE c.ComputeInfoID = a.ComputerInfoID),
       a.AlarmDataId, a.AlarmId, a.AlarmDate
FROM AlarmData a
WHERE a.ComputerInfoID = 1 
AND ( a.AlarmId = 2
      OR EXISTS ( SELECT 'a'
                  FROM AlarmData a2
                  WHERE a.ComputerInfoID = a2.ComputerInfoID
                  AND a2.AlarmId = 2
                  AND a2.AlarmDate > a.AlarmDate
                  AND NOT EXISTS ( SELECT 'b'
                                   FROM AlarmData a3
                                   WHERE a.ComputerInfoID = a3.ComputerInfoID
                                   AND a3.AlarmDate < a2.AlarmDate
                                   AND a3.AlarmDate > a.AlarmDate
                                  )
                )
     )
1
  • The previous alarm ID is not necessarily going to be the record numerically one less than the record that fits the criteria. It could be any number of record above.
    – Steven
    Apr 16, 2015 at 19:58
0

I also have given it a try. I created a SQLFiddle for it.

Things to notice. You can set the depth it goes back to show errors, so instead of only showing the last error you can also show the last two errors by simple increasing the depth to 2 here:

cte.Depth + 1 <= 1 -- Depth you want to go to

and here

alarms.Depth <= 1 -- Should equal the depth in the CTE query

When you run this query it give the following resultset:

ComputerInfoId  ComputerName    AlarmDataId AlarmId AlarmDate
1               Desktop1        3           7       2014-03-03 01:12:33.000
1               Desktop1        4           2       2014-03-03 01:12:36.000
1               Desktop1        6           12      2014-03-03 01:15:30.000
1               Desktop1        7           2       2014-03-03 01:16:12.000
0

you can use this query for your expected output

WITH cte
AS (
    SELECT AD.ComputerInfoId
        ,ComputerName
        ,AlarmDataId
        ,AlarmId
        ,AlarmDate
    FROM AlarmData AD
    INNER JOIN ComputerInfo C ON AD.ComputerInfoId = C.ComputerInfoId
    WHERE AD.ComputerInfoId = 1
        AND AlarmId <> 2
    )
SELECT ComputerInfoId
    ,ComputerName
    ,AlarmDataId
    ,AlarmId
    ,AlarmDate
FROM cte
WHERE AlarmDataId < (
        SELECT Max(AlarmDataId)
        FROM AlarmData
        WHERE ComputerInfoId = 1
            AND AlarmId = 2
        )

UNION ALL

SELECT AD.ComputerInfoId
    ,ComputerName
    ,AlarmDataId
    ,AlarmId
    ,AlarmDate
FROM AlarmData AD
INNER JOIN ComputerInfo C ON AD.ComputerInfoId = C.ComputerInfoId
WHERE AD.ComputerInfoId = 1
    AND AlarmId = 2
ORDER BY AlarmDataId
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.