Query with latest date

  • Thread starter Thread starter Ramesh
  • Start date Start date
R

Ramesh

HI ,

I have a table with Job ID, ActionDate and Action Taken.

Each JobID has many ActionDates and ActionTaken.

Can I write a query which will display only the last date in Job ID and the
Action Taken?

I am quite a novice in Access.

Thanks
Ramesh
 
Ramesh said:
HI ,

I have a table with Job ID, ActionDate and Action Taken.

Each JobID has many ActionDates and ActionTaken.

Can I write a query which will display only the last date in Job ID
and the Action Taken?

I am quite a novice in Access.

Thanks
Ramesh

Try something like this in the SQL view of a new query:

SELECT A.JobID, A.ActionDate, B.ActionTaken
FROM
(SELECT
YourTable.JobID,
Max(YourTable.ActionDate) AS ActionDate
FROM YourTable
GROUP BY YourTable.JobID) AS A
INNER JOIN
YourTable AS B
ON (A.ActionDate = B.ActionDate) AND (A.JobID = B.JobID);

You must replace the table name represented as "YourTable" in the above
SQL with the actual name of your table. Also, if I have the field names
wrong you must correct them.
 
Hi Dirk,

Looks like you have used two tables in your query. All 3 fields I have
mentioned comes from the same table. Though I do have another table with
the details of each Job ID, but I didnt mention that in my problem.

Can you help with the single table?

Thanks
Ramesh
 
Ramesh, there is only one table in Dirk's query.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Ramesh said:
Hi Dirk,

Looks like you have used two tables in your query. All 3 fields I
have mentioned comes from the same table. Though I do have another
table with the details of each Job ID, but I didnt mention that in my
problem.

Can you help with the single table?

No, Ramesh, the query only uses one table -- but it uses it twice, each
time with a different alias. That's what the "AS A" and "AS B" clauses
are doing: defining aliases or "alternate names" for the table and for
a subquery of the table. This is probably more complicated SQL than
you're familiar with, but let's take it apart and look at it from the
inside out.

Look at this part:

(SELECT
YourTable.JobID,
Max(YourTable.ActionDate) AS ActionDate
FROM YourTable
GROUP BY YourTable.JobID) AS A

The SELECT statement in parentheses defines a totals query that groups
the data in YourTable by JobID and returns one record for each unique
JobID in the table, containing the JobID and the maximum (i.e., latest)
ActionDate for that JobID. We take that totals query and give it the
alias "A", so that we can treat it as if it were a table itself.

If all we wanted was to get the latest ActionDate for each JobID, we'd
be done already. But unfortunately, we need to get the ActionTaken on
that date. For that we have to go back to the table again, and find the
record that matches the JobID and its maximum ActionDate. So we say:

INNER JOIN
YourTable AS B

This takes that "derived table" A and joins it to ... YourTable again!
To avoid any confusion in referring to this second copy of YourTable, we
give it a different alias; I chose B, for simplicity. So now we have
one set of records (from A) containing the JobID and ActionDate we're
interested in, and another set (from B) containing all the records, and
we match them up:

ON (A.ActionDate = B.ActionDate) AND (A.JobID = B.JobID);

The INNER JOIN discards any records from either A or B that don't find a
match. Of course, all records from A have matches, because they came
from YourTable in the first place, so it's really only the unwanted
records from B that get filtered out by this join. This gives us an
intermediate result set that contains the JobID and latest ActionDate
from A, and all the fields from the record in B that match that JobID
and ActionDate. From that set of records we select out the fields we
want:

SELECT A.JobID, A.ActionDate, B.ActionTaken
FROM
...

And we have what we want.
 
Wow, this is really beautiful.

I didnt have a clue Query in Access can be so interesting.

Thanks so much for the pains taken to explain that query and more for the
inspiration you caused to learn this stuff.

So one more request to you, can you guide me to how I could learn this.
Some site or a book? I have been struggling with queries for many months
now. All the Access books I have seen give just basic queries which dont
fulfil my requirements.

Thanks a million again.

Ramesh
 
Dirk,
Another probably silly question.

Is it possible to get the same thing done without using SQL by using the
Design view?

Thanks
Ramesh
 
Ramesh said:
Wow, this is really beautiful.

I didnt have a clue Query in Access can be so interesting.

Thanks so much for the pains taken to explain that query and more for
the inspiration you caused to learn this stuff.

So one more request to you, can you guide me to how I could learn
this. Some site or a book? I have been struggling with queries for
many months now. All the Access books I have seen give just basic
queries which dont fulfil my requirements.

Hmm, I'm largely self-taught as far as SQL goes, so I don't know
personally which books are really good. One that I've heard good things
about is _SQL Queries for Mere Mortals_, by Michael Hernandez and John
Viescas. Here's a link:


http://www.amazon.com/exec/obidos/tg/detail/-/0201433362/102-6547337-7011316?v=glance

In fact, one of these days I hope to get my hands on a copy.

There's also a pretty good, though technical section on SQL in the
_Access {97, 2000, 2002} Developer's Handbook_, by Getz et. al. Here's
a link:


http://www.amazon.com/exec/obidos/tg/detail/-/0782140114/103-2552250-6857469?v=glance

That is a terrific book for a developer, encompassing an enormous amount
of how-to wisdom, but be warned: it's on the technical side.

And for answers to a variety of specific questions about queries in
Access, see the Queries section at The Access Web:

http://www.mvps.org/access

There's a lot of information there on other Access topics, too.
Thanks a million again.

You're welcome.
 
Ramesh said:
Dirk,
Another probably silly question.

Is it possible to get the same thing done without using SQL by using
the Design view?

Yes, but it's a bit tricky. You can do it by "stacking queries"; that
is, by first building and saving the totals query, then taking that
saved query and using it as a source table for a second query that joins
it to the original table and selects the output fields.

Or you can do it with a single query, which you construct by first
building the totals query in the query designer, then flipping it into
SQL view, prefacing the SQL the query builder produced with

SELECT JobID, ActionDate FROM(

and appending

) AS A

to the end of it. Then you can switch back to design view, and you'll
just see the pseudo-table A in the upper pane. At that point, you can
add the original table to the query, draw join lines, and select the
ActionTaken field from that table to go along with the other fields. I
frequently work this way when I'm building a query that requires a
derived table.
 
Back
Top