Re: Complex Querys using instances of tables
- From: "John Spencer" <spencer@xxxxxxxxx>
- Date: Thu, 5 Jan 2006 08:51:03 -0500
I suggest you try it and see. If you get errors than post here.
Access will probably insist that you use the AS when naming the subquery.
Also, it may change the SQL format to use square brackets and a period
around the subquery vice the parentheses. So you may see the following.
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}. AS maxresults
WHERE rh.report_name = maxresults.report_name
AND rh.report_run_date= maxresults.maxdate;
Personnally I would use an inner join or a coordinated subquery in the WHERE
clause as I think they would be more efficient in Access then the cartesian
join created by the above query.
Subquery in Where clause approach.
SELECT rh.user_name, rh.report_name, rh.report_run_date
FROM report_history rh
WHERE rh.Report_run_date =
(SELECT max(report_run_date) as maxdate
FROM report_history
WHERE report_History.Report_name = rh.report_name)
Inner Join approach
SELECT rh.user_name, rh.report_name, rh.report_run_date
FROM report_history rh INNER JOIN
(SELECT max(report_run_date) as maxdate, report_name
FROM report_history
GROUP BY report_name) as maxresults
ON rh.report_name = maxresults.report_name
AND rh.report_run_date= maxresults.maxdate;
"Francis" <francis_1750@xxxxxxx> wrote in message
news:1136459400.541248.136930@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> *I studied a website who has tutors regarding SQL, but it uses more the
> ORACLE approach, and they often give complex examples like this, what i
> would appreciate is how to make the same work in Access so
> MicrosoftJetEngine recognizes it well:
>
> Question: I'm trying to pull some info out of a table. To simplify,
> let's say the table (report_history) has 4 columns:
>
> user_name, report_job_id, report_name, report_run_date.
>
> Each time a report is run in Oracle, a record is written to this table
> noting the above info. What I am trying to do is pull from this table
> when the last time each distinct report was run and who ran it last.
>
> My initial query:
>
> SELECT report_name, max(report_run_date)
> FROM report_history
> GROUP BY report_name;
>
> runs fine. However, it does not provide the name of the user who ran
> the report.
>
> Adding user_name to both the select list and to the group by clause
> returns multiple lines for each report; the results show the last time
> each person ran each report in question. (i.e. User1 ran Report 1 on
> 01-JUL-03, User2 ran Report1 on 01-AUG-03). I don't want that....I just
> want to know who ran a particular report the last time it was run.
>
> This is where things get a bit complicated. The SQL statement below
> will return the results that you want:
>
> 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;
>
> Ok now my questions:
> 1)instancing table report_history as rh (this works in access??)
> 2)Encapsulated a Select inside a FROM statement (this works in acess??)
> 3)How can we accomplish the same results in a similar query in access
> 2003.
>
> Thanks in advance
>
.
- Follow-Ups:
- Re: Complex Querys using instances of tables
- From: Francis
- Re: Complex Querys using instances of tables
- From: Francis
- Re: Complex Querys using instances of tables
- From: Francis
- Re: Complex Querys using instances of tables
- References:
- Complex Querys using instances of tables
- From: Francis
- Complex Querys using instances of tables
- Prev by Date: Re: dcount with more than one criteria
- Next by Date: Re: Help with my first Delete Query
- Previous by thread: Complex Querys using instances of tables
- Next by thread: Re: Complex Querys using instances of tables
- Index(es):
Relevant Pages
|