Re: Get the latest status

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Use a subquery.
Something like this:

SELECT id, start_dt, end_dt,
(SELECT TOP 1 status
FROM tbl2
WHERE tbl2.id = tbl1.id
ORDER BY tbl2.stats_change_dt DESC, tbl2.id) AS TheStatus
FROM tbl1;

If subqueries are new, here's an introduction:
http://allenbrowne.com/subquery-01.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"JoZ via AccessMonster.com" <u11274@uwe> wrote in message
news:90f0c7508f141@xxxxxx
I have two tables:
tbl1: id, start_dt, end_dt
tbl2, id, status, status_change_dt

tbl2 can have multiple rows for each id to show an id's different status
changed at different time. I need to join the two tbls and then only get the
latest status for each id. I can do two queries in Access (step1: Join 2tbls,
group and sort by Ascending; step2: get the 1st record of qry1). Can someone
write a SQL to do the same? I need to bring it to a unix environment. thx.

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/access-queries/200901/1


.



Relevant Pages

  • Re: Error using UPDATE statement
    ... SELECT subquery. ... UPDATE Tbl1 ... SET Row2 = '1' ... FROM Tbl2 ...
    (comp.databases.ms-sqlserver)
  • Re: Foreign Key problem
    ... If you want to create a record in tbl2 as soon as a new record is entered in tbl1, use the AfterInsert event procedure of the *form* where the data is added. ... updated with a primary key and matching foreign key on insert. ...
    (microsoft.public.access.formscoding)
  • Re: Monthly Total
    ... Can we assume there is relationship between tbl1 and tbl2 based on the ID, ... Accept Group By in the Total row under this field. ... ID1 Date Error ...
    (microsoft.public.access.queries)
  • Re: linking two forms (2 cont)
    ... Andrey, ... from tbl1 to tbl2 using Tools/Relationship. ... That way, whenever you create a new record in tbl1, a corresponding ... Candia Computer Consulting - Candia NH ...
    (microsoft.public.access.forms)
  • Re: want comments to show 1x not 2xs
    ... Something is wrong in the design of your report! ... Base the main report on tbl1 ... and a subreport on tbl2 and comments will only come out once. ... > we have some clients that may leave the program and come> back months later,so if a client leaves and comes back> there are 2 entry dates, which is fine, the problem is ...
    (microsoft.public.access.formscoding)