But how do you find
Using multiple queries and a program, there are many ways to perform this type of data retrieval - but I want to do this with a single query.
This is one of those areas where different databases implement the function in different ways.
Simple "group by"
select "Name", max("Date_Time") from "Programs_tbl"
group by "Name"
But it can not be directly extended to include other fields.
Let the "where" Clause Contain a Second Query
select * from "Programs_tbl" xx
where xx."Date_Time" = (select max("Date_Time") from "Programs_tbl" yy
where xx."Name" = yy."Name")
Notice that because the same table is used in both queries, the 2 result sets must
be assigned aliases - xx and yy in this example.
Place the Second Query in a "Join"
SELECT rh.user_name, rh.report_name, rh.report_run_date
FROM report_history rh,
(SELECT max(report_run_date) as maxdate, report_name
FROM report_history
GROUP BY report_name) maxresults
WHERE rh.report_name = maxresults.report_name
AND rh.report_run_date = maxresults.maxdate;
According to
Introduction to PostgreSQL SQL -
Nested subqueries and derived tables,
this imbedded query is known as either
a derived table or a temporary view.
Author: Robert Clemenzi -
clemenzi@cpcug.org