473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

remove duplicated fields depending on time

hello,

i am new enough to sql and looking for help with the following..

I have a table with duplicate record information in some fields which i dont want 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 say 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…

First i want to compare the tool number and if this is the same.. then i want to compare the alarmtext and if this is the same... then i want to compare the times... now if the times are within 5 mins of each other i only want to show the first time occured..

this is what i want the table to show

Tool time_occured alarmtext

#1 12:20 DOOR OPEN
#1 12:30 DOOR OPEN
#2 12:20 DOOR OPEN

thanks
May 14 '09 #1
3 1226
balame2004
142 100+
I don't think it is easy to do with siongle query. Better to think about a stored procedure to do it.
May 14 '09 #2
any ideas of a stored procedure i could use?
May 15 '09 #3
ck9663
2,878 Expert 2GB
Here, try this. I included some display queries...

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @tblAlarm table (Tool varchar(2), time_occured varchar(5), alarmtext varchar(10))
  3.  
  4. insert into @tblAlarm (Tool, time_occured, alarmtext)
  5. values ('#1', '12:20', 'DOOR OPEN')
  6. insert into @tblAlarm (Tool, time_occured, alarmtext)
  7. values ('#1', '12:21', 'DOOR OPEN')
  8. insert into @tblAlarm (Tool, time_occured, alarmtext)
  9. values ('#1', '12:30', 'DOOR OPEN')
  10. insert into @tblAlarm (Tool, time_occured, alarmtext)
  11. values ('#2', '12:20', 'DOOR OPEN')
  12. insert into @tblAlarm (Tool, time_occured, alarmtext)
  13. values ('#2', '12:24', 'DOOR OPEN')
  14.  
  15. select tool, time_occured, cast(time_occured as datetime), alarmtext from @tblAlarm 
  16.  
  17. select a1.tool, a1.time_occured, a1.alarmtext, 
  18. prev_alarm = (select top 1 a2.time_occured from @tblAlarm a2 where a1.tool = a2.tool and cast(a1.time_occured as smalldatetime) > cast(a2.time_occured as smalldatetime) order by cast(a2.time_occured as smalldatetime) desc)
  19. from @tblAlarm a1
  20.  
  21.  
  22. select * from
  23. (select a1.tool, a1.time_occured, a1.alarmtext, 
  24. prev_alarm = (select top 1 a2.time_occured from @tblAlarm a2 where a1.tool = a2.tool and cast(a1.time_occured as smalldatetime) > cast(a2.time_occured as smalldatetime) order by cast(a2.time_occured as smalldatetime) desc)
  25. from @tblAlarm a1) alarms
  26. where prev_alarm is null or datediff(mi,cast(prev_alarm as smalldatetime), cast(time_occured as smalldatetime)) > 5
  27.  
  28.  
Happy Coding!

--- CK
May 15 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Wm | last post by:
I have a table of users in mySQL that appears to have a lot of duplicates. What's the best way to look at the userID and email and delete the duplicates? Thanx, Wm
8
by: Michelle | last post by:
hi, i have created an array from recordset containing user names eg. (davidp, davidp, evenf, patricka, rebeccah) which i have sorted in alphabetical order, but i need to identify duplicates...
6
by: allyn44 | last post by:
HI--what I am trying to do is 2 things: 1. Open a form in either data entry mode or edit mode depending on what task the user is performing 2. Cancel events tied to fields on the form if I am in...
1
by: Ellis Yu | last post by:
Dear all, I write my own components for my project and add them in "My User Control" under the toolbar. Some time later, I make some other new components into it and want to use them in my...
18
by: TORQUE | last post by:
Hi, Im wondering if anyone can help me with a problem. I have a form with more than 50 unbound fields. Some of the fields will be blank from time to time. This seems to be where im having...
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
37
by: bahoo | last post by:
Hi, I have a list like and as output I want If I myList.remove('0024') then only the first instance of '0024' is removed.
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.