•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Database Design section within the Web Development category of DaniWeb, a massive community of 456,467 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,768 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Database Design advertiser: Programming Forums
Views: 1701 | Replies: 9
![]() |
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hello,
I'm working with SQL server 2005 and a database that has "equipment status" data that is spread across several tables and is time stamped in only one table, there are multiple instances of duplicate records with differing time stamps.
I need to select only the newest time stamped messages for each piece of equipment(a.resource), the criteria is: I need to return b.message_text, a.host, a.resource where a.message_num = b.message_num and the newest a.event_in_secs.
I’m new to SQL server (and databases, sql) and guess ‘cursor’ may be the way to go but I would appreciate any help. Thanks
I'm working with SQL server 2005 and a database that has "equipment status" data that is spread across several tables and is time stamped in only one table, there are multiple instances of duplicate records with differing time stamps.
I need to select only the newest time stamped messages for each piece of equipment(a.resource), the criteria is: I need to return b.message_text, a.host, a.resource where a.message_num = b.message_num and the newest a.event_in_secs.
I’m new to SQL server (and databases, sql) and guess ‘cursor’ may be the way to go but I would appreciate any help. Thanks
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation:
Rep Power: 7
Solved Threads: 59
•
•
•
•
I’m new to SQL server (and databases, sql) and guess ‘cursor’ may be the way to go but I would appreciate any help. Thanks
Cursor? yuck! I have been programing SQL Server for 8 years and I have *never* used a cursor they are slooooowww and are completely contrary to set based theory. If you *have* to iterate data in a one by one scenario do it in code where it belongs not in the database. Or use a TSQL while loop or table variables.
You need to look into the GROUP BY clause of the SQL syntax.
Select
b.message_text, a.host, a.resource,
max(a.event_in_secs) as max_event_in_secs
from
a join b on a.message_num = b.message_num
group by
b.message_text, a.host, a.resource Last edited by hollystyles : Sep 13th, 2007 at 5:30 am.
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks for the help, I've only been at this a real short while - sorry if I didn't make sense, I only program in LabVIEW so I'm new to sql, but now I know the best path to pursue, I'm glad I didn't waste a bunch of time researching 'cursors'.
I'll try to clarify my task a little better..... I have a constant stream of data getting put into several different tables from thousands of different pieces of equipment throughout a very large satellite network. I need only 2 of the tables for my query, the first table (call it A) has 2 columns (message_text, message_num), the other (call it B) has several columns but I need only 4 of them (event_date, event_in_secs, message_num, resource).
I need to return the most current record for each piece of equipment (which is defined in B.resource) containing: A.message_text, B.event_date, B.resource.
The criteria is where A.message_num = B.message_num (this will return several dozen duplicates for each B.resource) and the latest (top?) B.event_in_secs for each instance of B.resource.
I hope this makes sense - thanks again (sucks to be a newbee)
I'll try to clarify my task a little better..... I have a constant stream of data getting put into several different tables from thousands of different pieces of equipment throughout a very large satellite network. I need only 2 of the tables for my query, the first table (call it A) has 2 columns (message_text, message_num), the other (call it B) has several columns but I need only 4 of them (event_date, event_in_secs, message_num, resource).
I need to return the most current record for each piece of equipment (which is defined in B.resource) containing: A.message_text, B.event_date, B.resource.
The criteria is where A.message_num = B.message_num (this will return several dozen duplicates for each B.resource) and the latest (top?) B.event_in_secs for each instance of B.resource.
I hope this makes sense - thanks again (sucks to be a newbee)
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation:
Rep Power: 7
Solved Threads: 59
Samson, please don't 'hi-jack' threads. Here are the forum rules:
http://www.daniweb.com/forums/faq.ph...niweb_policies
And here is a guide on how to post your own thread:
http://www.daniweb.com/forums/faq.php?faq=daniweb_howto
http://www.daniweb.com/forums/faq.ph...niweb_policies
And here is a guide on how to post your own thread:
http://www.daniweb.com/forums/faq.php?faq=daniweb_howto
Last edited by hollystyles : Oct 15th, 2007 at 4:56 am.
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks,
Here's what I came up with - 5 lines and works great..........
select distinct atnm_log.resource, max(atnm_log.event_in_secs), message_table.message_text
from atnm_log, message_table
where atnm_log.message_num = message_table.message_num
and atnm_log.resource like '%par[0-3]%' or atnm_log.resource like '%par[12-13]%'
group by atnm_log.resource, message_table.message_text
Thanks for the help!
Here's what I came up with - 5 lines and works great..........
select distinct atnm_log.resource, max(atnm_log.event_in_secs), message_table.message_text
from atnm_log, message_table
where atnm_log.message_num = message_table.message_num
and atnm_log.resource like '%par[0-3]%' or atnm_log.resource like '%par[12-13]%'
group by atnm_log.resource, message_table.message_text
Thanks for the help!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Database Design Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- sql query problem with MS Access and C# (C#)
- Urgent Help with SQL Query (MySQL)
- C# VS 2005 - SQL Query Parameters to an ODBC DataSource (C#)
- sql query updating problem (Visual Basic 4 / 5 / 6)
- Javascript array from sql query (JSP)
- Please help me out with MySQL query (MySQL)
- PHP/SQL query help (PHP)
- Retreiving variables from a sql query into a form (PHP)
Other Threads in the Database Design Forum
- Previous Thread: SQL Books
- Next Thread: How do I divide the sum of 2 columns in sql 2000 server




Linear Mode