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 > MySQL > Question about MAX function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-05-07, 17:36
stevieooo stevieooo is offline
Registered User
 
Join Date: Feb 2007
Posts: 13
Question about MAX function

Hi all. I have a VB6 application that accesses data from a MySql table and returns it. One of my columns is time_date that puts a timestamp when data is uploaded. If I have more than one recordset that will be returned, I only want the one with the greatest timestamp. Here is my query:
Code:
select reference,item1,item2,item3,item4,user_name,max(time_date) 
from item_tracker where reference like ('" & txtReference.Text & "')  
group by time_date
The recordset that gets returned is the first one in the table and not the one with the latest timestamp. Can anyone help me so I can return the newest record(max(timestamp))? Thanks.
Reply With Quote
  #2 (permalink)  
Old 03-05-07, 18:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
select reference,item1,item2,item3,item4,user_name,time_d ate
from item_tracker
where reference like ('" & txtReference.Text & "')
and time_date = ( select max(time_date) from item_tracker )

this is not a GROUP BY problem
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-06-07, 08:42
stevieooo stevieooo is offline
Registered User
 
Join Date: Feb 2007
Posts: 13
Thank you ever so much. I was trying to make a mountain out of a molehill!!!
Reply With Quote
  #4 (permalink)  
Old 03-09-07, 11:51
snorp snorp is offline
Registered User
 
Join Date: Apr 2004
Location: Europe->Sweden->Stockholm
Posts: 71
About MySQL and GROUP

A recommendation related to your problem is to set "sql_mode" to, among other things, ONLY_FULL_GROUP_BY. This settings requires all columns on which aggregation functions are not applied to be specified in the GROUP BY clause. Your query would have terminated with an error since you did not mention all columns in your GROUP BY. Have a look in the documentation and read about the "modes" to see how you can make MySQL stricter (which prevents you from running some queries that would otherwise give you unexpected results).
Reply With Quote
  #5 (permalink)  
Old 03-09-07, 11:55
stevieooo stevieooo is offline
Registered User
 
Join Date: Feb 2007
Posts: 13
Thank you for the tip. I will use the only_full_group_by mode.
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