Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / November 2005

Tip: Looking for answers? Try searching our database.

Two queries into one

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick S. - 23 Nov 2005 15:26 GMT
I have a table Invoices with columns Date1, Account and Amount, e.g.

Date1       Account   Amount
3.3.2004     3       100
5.3.2004     2        20
18.5.2004      2        30
3.10.2004     1        10
18.11.2004     1       500
18.1.2005     3        15

I'd like to find for each account the row with the latest date for that
account.
The result should be:

18.11.2004   1  500
18.5.2004    2   30
18.1.2005    3   15

The query comprising of two steps and an intermediate table is easy:

first

SELECT Max(Date1) AS NewDate, Account AS NewAccount
INTO tbl1
FROM Invoices
GROUP BY Account;

then

SELECT Invoices.Date1, Invoices.Account, Invoices.Amount
FROM Invoices INNER JOIN tbl1
ON (Invoices.Account=tbl1.NewAccount) AND (Invoices.Date1=tbl1.NewDate);

How would that go in one step?

TIA,
Nick
Marshall Barton - 23 Nov 2005 16:03 GMT
>I have a table Invoices with columns Date1, Account and Amount, e.g.
>
[quoted text clipped - 30 lines]
>
>How would that go in one step?

SELECT T.Date1, T.Account, T.Amount
FROM Invoices As T
WHERE  T.Date1=(SELECT Max(X.Date1)
                FROM  Invoices As X
                WHERE T.Account=X.NewAccount)

Signature

Marsh
MVP [MS Access]

Nick S. - 23 Nov 2005 16:17 GMT
> SELECT T.Date1, T.Account, T.Amount
> FROM Invoices As T
> WHERE  T.Date1=(SELECT Max(X.Date1)
>                 FROM  Invoices As X
>                 WHERE T.Account=X.NewAccount)


Thanks a lot, Marsh. With the last line changed to
   WHERE T.Account=X.Account)
it works like a dream.

Nick
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.