Re: Query Problem..
- From: "Bob Barrows [MVP]" <reb01501@xxxxxxxxxxxxxxx>
- Date: Thu, 29 Jun 2006 07:26:51 -0400
AJ wrote:
Hi all,I can't test it, but try this:
I am trying to execute the following query:
SELECT
c.ID, c.Company_Name,
(SELECT Max(ID) FROM Sale s WHERE s.Company_ID = c.ID) AS
LastSaleID, (SELECT s.Package_ID FROM Sale s WHERE s.ID =
LastSaleID) AS PackageID
FROM
Company AS c
My problem is, 'LastSaleID' isn't picked up as a column in of the
main query in the second sub query. If i was to use another column in
the main query this problem dissappears.
I am unfortunatel using Access; any help appreciated to get this to
work as needed.
SELECT
c.ID, c.Company_Name,
s.LastSaleID,
p.PackageID
FROM
(Company AS c INNER JOIN
(SELECT Company_ID, Max(ID) AS LastSaleID FROM Sale
GROUP BY Company_ID) As s
ON s.Company_ID = c.ID) INNER JOIN
Sale p ON p.ID = s.LastSaleID
If Access complains about the subquery in thre FROM clause, create a saved
query called MaxIDPerCompany with this sql:
SELECT Company_ID, Max(ID) AS LastSaleID FROM Sale
GROUP BY Company_ID
And change the above query to:
SELECT
c.ID, c.Company_Name,
s.LastSaleID,
p.PackageID
FROM
(Company AS c INNER JOIN
MaxIDPerCompany As s
ON s.Company_ID = c.ID) INNER JOIN
Sale p ON p.ID = s.LastSaleID
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
.
- Follow-Ups:
- Re: Query Problem..
- From: AJ
- Re: Query Problem..
- Prev by Date: Re: Help with global.asa & sql database
- Next by Date: Re: Query Problem..
- Previous by thread: Help with global.asa & sql database
- Next by thread: Re: Query Problem..
- Index(es):
Relevant Pages
|