Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql to remove repeat data within specific

Author  Topic 

parabolam
Starting Member

4 Posts

Posted - 2009-05-14 : 06:20:31
hey all..

im new enough with sql language and need help with a query that i am trying to show..

i pull alarm data from a database and it gives me all the alarms that occur with the following columns..

Tool time_occured alarmtext


When I run the query say it gives me 5 alarms….


Tool time_occured alarmtext

#1 12:20 DOOR OPEN

#1 12:21 DOOR OPEN

#1 12:30 DOOR OPEN

#2 12:20 DOOR OPEN

#2 12:24 DOOR OPEN



What I need is the table to show the following…


Tool time_occured alarmtext

#1 12:20 DOOR OPEN

#1 12:30 DOOR OPEN

#2 12:20 DOOR OPEN



I want to show alarms for tools… but not show an alarm if the same alarm occurs within 5 minutes of the first alarm of that type on the unique tool…

Is this doable??

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-14 : 10:17:28
[code]SELECT Tool,time_occured,alarmtext
FROM
(
SELECT t.Tool,t.time_occured,t.alarmtext,(SELECT TOP 1 time_occured FROM Table WHERE Tool=t.Tool AND time_occured<t.time_occured ORDER BY time_occured DESC) AS Prev_time_occured FROM Table t
)r
WHERE DATEDIFF(ss,ISNULL(Prev_time_occured,0), time_occured)>=5
[/code]
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-05-14 : 10:40:01
Recursion will work with SQL2005/8:

-- *** Test Data ***
DECLARE @t TABLE
(
ToolId int NOT NULl
,time_occurred datetime NOT NULL
,alarmtext varchar(50) NOT NULL
)
INSERT INTO @t
SELECT 1, '20090514 12:20', 'DOOR OPEN' UNION ALL
SELECT 1, '20090514 12:22', 'DOOR OPEN' UNION ALL
SELECT 1, '20090514 12:30', 'DOOR OPEN' UNION ALL
SELECT 2, '20090514 12:20', 'DOOR OPEN' UNION ALL
SELECT 2, '20090514 12:24', 'DOOR OPEN'
-- *** Test Data ***

;WITH cte (ToolId, time_occurred, alarmtext)
AS
(
SELECT ToolId, time_occurred, alarmtext
FROM
(
SELECT ToolId, time_occurred, alarmtext
,ROW_NUMBER() OVER (PARTITION BY ToolId, alarmtext ORDER BY time_occurred) AS RowNum
FROM @t
) D1
WHERE RowNum = 1

UNION ALL

SELECT ToolId, time_occurred, alarmtext
FROM
(
SELECT T.ToolId, T.time_occurred, T.alarmtext
,ROW_NUMBER() OVER (PARTITION BY T.ToolId, T.alarmtext ORDER BY T.time_occurred) AS RowNum
FROM @t T
JOIN cte C
ON T.ToolId = C.ToolId
AND T.alarmtext = C.alarmtext
AND T.time_occurred >= DATEADD(minute, 5, C.time_occurred)
) D2
WHERE RowNum = 1
)
SELECT *
FROM cte
ORDER BY ToolId, time_occurred, alarmtext

Go to Top of Page

parabolam
Starting Member

4 Posts

Posted - 2009-05-15 : 07:04:09
thanks for the replys... i'm using SQL developer so cant use Ifor's reply..

Visakh16,

Here is what i use to pull the data... how do i fit in your code?

SELECT
t.tool
,t.time_occurred
,t.alarmtext

FROM
alarmhist t

WHERE

AND t.tool = 'CABINET'
AND t.time_occurred >= (DATE - 5/24/60)
ORDER BY
2 Asc
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-15 : 13:01:40
[code]SELECT tool, time_occurred, alarmtext
FROM
(
SELECT
t.tool
,t.time_occurred
,t.alarmtext
,(SELECT TOP 1 time_occured FROM alarmhist WHERE Tool=t.Tool AND time_occured<t.time_occured ORDER BY time_occured DESC) AS Prev_time_occured
FROM
alarmhist t

WHERE

AND t.tool = 'CABINET'
AND t.time_occurred >= (DATE - 5/24/60)
)r
WHERE DATEDIFF(ss,ISNULL(Prev_time_occured,0), time_occured)>=5
ORDER BY
2 Asc
[/code]
Go to Top of Page
   

- Advertisement -