Selecting Max Valued Result From Joined Table

This is a discussion on Selecting Max Valued Result From Joined Table within the MySQL Database forums, part of the Database Forums category; I am trying to select a single entry from the "process_account_inspections" where the column "timestamp" is ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 5 Days Ago
Chuck Pearce
 
Posts: n/a
Default Selecting Max Valued Result From Joined Table

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 (permalink)  
Old 5 Days Ago
Paul Lautman
 
Posts: n/a
Default Re: Selecting Max Valued Result From Joined Table

Chuck Pearce wrote:
>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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:35 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0