User Name Password Register
DaniWeb IT Discussion Community
All
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,442 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,610 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: 1700 | Replies: 9
Reply
Join Date: Sep 2007
Posts: 4
Reputation: bnilsen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bnilsen bnilsen is offline Offline
Newbie Poster

Question I could use help for a sql query

  #1  
Sep 13th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Posts: 70
Reputation: zmariow is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
zmariow's Avatar
zmariow zmariow is offline Offline
Junior Poster in Training

Re: I could use help for a sql query

  #2  
Sep 13th, 2007
Do you know what are "INNER JOIN", "OUT JOIN" in SQL???

If no, then learn those concepts first and you'll be able to solve your problem (I think).

If you do, well... can you clarify your question a little more

Good luck
Last edited by zmariow : Sep 13th, 2007 at 3:47 am.
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: I could use help for a sql query

  #3  
Sep 13th, 2007
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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Sep 2007
Posts: 4
Reputation: bnilsen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bnilsen bnilsen is offline Offline
Newbie Poster

Re: I could use help for a sql query

  #4  
Sep 14th, 2007
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)
Reply With Quote  
Join Date: Aug 2007
Posts: 70
Reputation: zmariow is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
zmariow's Avatar
zmariow zmariow is offline Offline
Junior Poster in Training

Re: I could use help for a sql query

  #5  
Sep 14th, 2007
This query will help you get all the rows you need with duplicate entries and without selecting the most current record:

SELECT A.message_text, B.event_date, B.resource
FROM A INNER JOIN B
ON A.message_num = B.message_num

It's not your end query but it's a good start
Good luck.
Reply With Quote  
Join Date: Sep 2007
Posts: 4
Reputation: bnilsen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bnilsen bnilsen is offline Offline
Newbie Poster

Re: I could use help for a sql query

  #6  
Sep 14th, 2007
Thanks for the tip, I'll try it when I get back to work.
Reply With Quote  
Join Date: Oct 2007
Posts: 4
Reputation: Samson_ZXL is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Samson_ZXL Samson_ZXL is offline Offline
Newbie Poster

Re: I could use help for a sql query

  #7  
Oct 14th, 2007
Hi guys!
I'm a new comer of this forum. I'm interested in sql statement :join. And can you show me the exact difference between inner join and left join ?

Thanks.
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: I could use help for a sql query

  #8  
Oct 15th, 2007
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
Last edited by hollystyles : Oct 15th, 2007 at 4:56 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Nov 2007
Posts: 34
Reputation: mohanrobin has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 0
mohanrobin mohanrobin is offline Offline
Banned

Re: I could use help for a sql query

  #9  
Nov 14th, 2007
The database is avast thing that connects several data and program related to that data.so
for your problem you have to study the inner join and outer join of the data base design.
Reply With Quote  
Join Date: Sep 2007
Posts: 4
Reputation: bnilsen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bnilsen bnilsen is offline Offline
Newbie Poster

Solution Re: I could use help for a sql query

  #10  
Nov 14th, 2007
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Database Design Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Database Design Forum

All times are GMT -4. The time now is 1:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC