Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SubQuery Syntax

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
I have a field that is defined by a subquery. In the subquery I am using the Where statement. In my where statement I am referencing a field in my main query.

Subquery:
(Select Total from MainQueryName Where ID = "I'm lost")

After ID equal is where I get lost trying to reference a field in the main query.

I hope I am making myself clear. ljprodev@yahoo.com
ProDev, MS Access Applications B-)
 
I'm not the greatest SQL writer, but I think that's an illegal circular reference. Your query design environment might help you to do things more graphically, then, once that's working right, goto View|SQL to see the code and learn the syntax a little better. Try making a new query that gets your "base" view, then make another query with the first qry as it's data source. It's a simple way to nest select statements.
 
Here is some general syntax to reference fields in the inner and outer query - use alias on the table name.

SELECT X.segmentCode, X.customerPO, X.customerID,
(SELECT top 1 Y.segmentName
FROM Detail850PO1 AS Y
where Y.seqmentName = X.segmentName ) AS Expr1
FROM Detail850PO1 AS X;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top