If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Nightmare Select Statement!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-05, 10:42
iblair iblair is offline
Registered User
 
Join Date: Sep 2005
Posts: 16
Nightmare Select Statement!

At least for my ability anyway!

I have a table which has the following columns (datacentre,machine,uptime,last_update), into which a row is inserted every 5 minutes for each machine in each datacentre, and the last_update column is the database system timestamp value. I would like to display the latest row for each machine using one SQL statement on DB2, but am having no luck so far!

Ian
Reply With Quote
  #2 (permalink)  
Old 10-03-05, 14:13
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Ian,
Something like this should be what you need:

Select datacenter,machine,max(last_update) from mytable group by datacenter,machine

Andy
Reply With Quote
  #3 (permalink)  
Old 10-04-05, 10:17
iblair iblair is offline
Registered User
 
Join Date: Sep 2005
Posts: 16
Thanks Andy.

This displays the last row for each machine, but omits the 'uptime' column. How would I also get it to also display the 'uptime' column for every row? I could easily do it with a couple of queries, but would prefer to get it all done in one for speed reasons.

Thanks,
Ian
Reply With Quote
  #4 (permalink)  
Old 10-04-05, 10:23
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Do the uptime values change with the last_update column?

Andy
Reply With Quote
  #5 (permalink)  
Old 10-05-05, 07:05
iblair iblair is offline
Registered User
 
Join Date: Sep 2005
Posts: 16
yes, last_update is the time when the row was inserted into the database (every five minutes for each machine), whereas uptime is the time the machine has been up for. If the machine is rebooted the uptime could go from several months to a few seconds. This is why I want to query for the latest uptime for each machine.

Thanks,
Ian
Reply With Quote
  #6 (permalink)  
Old 10-05-05, 07:55
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
OK, try something like this:

with t1 (datacenter,machine,last_update) as (Select datacenter,machine,max(last_update) from mytable group by datacenter,machine) select t1.datacentr,t1.machine,mt.uptime,t1.last_update from t1 inner join mytable as mt on (t1.datacenter = mt.datacenter and t1.machine = mt.machine and t1.last_update = mt.last_update)

HTH

Andy
Reply With Quote
  #7 (permalink)  
Old 10-05-05, 08:25
iblair iblair is offline
Registered User
 
Join Date: Sep 2005
Posts: 16
Talking Thank you :)

Thanks Andy, that's perfect!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On