Re: Select max row
| From: | Dan Koken | Date: | Mon, 05 Aug 2002 19:03:38 +0000 |
| Subject: | Re: Select max row | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-21002@lists.php.net to get a copy of this message | ||
Lisi:
If you have sub-selects then try something like this:
This will give you a list of the most recent date for each project and the latest stop for that date.
SELECT *
FROM timesheet t1
WHERE date = (SELECT max(date) as max_date
FROM timesheet t2
WHERE t2.project = t1.project)
AND stop = (SELECT max(stop)
FROM timesheet t3
WHERE t3.project = t1.project
AND t3.date = t2.max_date)
If you do not have sub-select you have to do it in a couple passes and create temp tables.
If you need help on how to formulate that.. Let me know.
HTH ..
Dan.
wrote:
I have a timesheet table with the following columns: project month date start stop company_ID There can be more than one start/stop entry for each date. I want to select the most recent date, as well as the latest stop time for that date. I tried the following query: SELECT date, stop FROM timesheet WHERE date = max(date) Which did not work. The error was Invalid use of group function. I think I need to do a join of the table to itself, but I'm not sure exactly how to formulate the query. Any suggestions are much appreciated. If the problem is not clear, please let me know what to clarify. Thanks, -Lisi