dbTalk Databases Forums  

Selecting Max Valued Result From Joined Table

comp.databases.mysql comp.databases.mysql


Discuss Selecting Max Valued Result From Joined Table in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Chuck Pearce
 
Posts: n/a

Default Selecting Max Valued Result From Joined Table - 05-07-2008 , 01:55 PM






I am trying to select a single entry from the
"process_account_inspections" where the column "timestamp" is the Max
(resulting in the most recent inspection). My code is below, however I
can't find how to select the max timestamp with each row returned. Any
suggestions? Currently it just returns the lowest entry in the table.
I tried to GROUP BY accounts.account_id HAVING MAX(InspectionDate) but
that didn't work.

SELECT
accounts.*,
franchisees.*,
offices.*,
process_account_inspections.`timestamp` AS InspectionDate,
process_account_inspections.inspection_score AS InspectionScore,
process_account_inspections.satisfactory AS Satisfactory
FROM accounts
INNER JOIN franchisees ON accounts.franchisee_id_FK =
franchisees.franchisee_id
INNER JOIN offices ON franchisees.office_id_FK = offices.office_id
LEFT JOIN process_account_inspections ON accounts.account_id =
process_account_inspections.account_id_FK
GROUP BY accounts.account_id
ORDER BY process_account_inspections.`timestamp` ASC

Thank you,
Chuck

Reply With Quote
  #2  
Old   
Paul Lautman
 
Posts: n/a

Default Re: Selecting Max Valued Result From Joined Table - 05-07-2008 , 03:07 PM






Chuck Pearce wrote:
Quote:
I am trying to select a single entry from the
"process_account_inspections" where the column "timestamp" is the Max
(resulting in the most recent inspection). My code is below, however I
can't find how to select the max timestamp with each row returned. Any
suggestions? Currently it just returns the lowest entry in the table.
I tried to GROUP BY accounts.account_id HAVING MAX(InspectionDate) but
that didn't work.

SELECT
accounts.*,
franchisees.*,
offices.*,
process_account_inspections.`timestamp` AS InspectionDate,
process_account_inspections.inspection_score AS InspectionScore,
process_account_inspections.satisfactory AS Satisfactory
FROM accounts
INNER JOIN franchisees ON accounts.franchisee_id_FK =
franchisees.franchisee_id
INNER JOIN offices ON franchisees.office_id_FK = offices.office_id
LEFT JOIN process_account_inspections ON accounts.account_id =
process_account_inspections.account_id_FK
GROUP BY accounts.account_id
ORDER BY process_account_inspections.`timestamp` ASC

Thank you,
Chuck
Wow, it's been a little while since this question has popped up. At least a
few weeks!

http://dev.mysql.com/doc/refman/5.0/...group-row.html




Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.