If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > I have a Subquery (nested) in Access can't figure it out in ASP/vbscript

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-20-02, 16:51
newknew newknew is offline
Registered User
 
Join Date: Dec 2002
Posts: 5
I have a Subquery (nested) in Access can't figure it out in ASP/vbscript

In Access, I have a series of queries that are required by the main query because of multiple autolookups that are based on the same key in the lookup table. Let me explain:

I have a table of Shipments. The Shipments table has fields for Customer, Shipper, Consignee, and BillTo. These fields are all related to the same ID field in a table called Locations and likewise store the ID of the associated Location. Therefore, a single record in Shipments could have the same ID from Locations for Customer, Shipper, Consignee and/or BillTo.

I want to use a lookup query to display the full name of the Location. In Access, I have created a set of subqueries to get the desired result. Subqueries are required because the Customer, Shipper, Consignee, and BillTo fields are ALL related to the same ID in Locations. Otherwise, Access is confused by ambiguous outer joins. In Access, I have achieved the desired result with no problems:

SELECT [shipments].[CustomerID], [locations].[Name], [shipments].[shipperid], [locations1].[Name], [shipments].[consigneeid], [locations2].[Name] FROM ((shipmentsLEFT JOIN locations ON [shipments].CustomerID]=[locations].[ID]) LEFT JOIN locations1 ON shipments].[shipperid]=[locations1].[ID]) LEFT JOIN locations2 ON shipments].[consigneeid]=[locations2].[ID];

(locations1 and locations2 are the names of the subqueries, or pre-queries... they are identical and look like this:

SELECT locations.* FROM locations;

)

Now, how do I port this to SQL so that I can use it in my VBScript/ASP code on my website? I would like to get the desired recordset with one SQL as opposed to running a quick "lookup query" everytime I need to lookup the name for an associated ID -- this would occur about 8 times on a page for the same record.

Thanks,
Tim
Reply With Quote
  #2 (permalink)  
Old 01-06-03, 11:23
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: I have a Subquery (nested) in Access can't figure it out in ASP/vbscript

I don't understand why you should need pre-queries anyway. Why not just do this:

SELECT [shipments].[CustomerID], [locations].[Name], [shipments].[shipperid], [locations1].[Name], [shipments].[consigneeid], [locations2].[Name] FROM ((shipmentsLEFT JOIN locations ON [shipments].CustomerID]=[locations].[ID]) LEFT JOIN locations locations1 ON shipments].[shipperid]=[locations1].[ID]) LEFT JOIN locations locations2 ON shipments].[consigneeid]=[locations2].[ID];

i.e. use aliases.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 01-06-03, 12:29
newknew newknew is offline
Registered User
 
Join Date: Dec 2002
Posts: 5
Re: I have a Subquery (nested) in Access can't figure it out in ASP/vbscript

locations1 and locations2 *are* the subqueries
Reply With Quote
  #4 (permalink)  
Old 01-06-03, 12:32
newknew newknew is offline
Registered User
 
Join Date: Dec 2002
Posts: 5
Re: I have a Subquery (nested) in Access can't figure it out in ASP/vbscript

locations1 and locations2 *are* the subqueries and this solution still uses them
Reply With Quote
  #5 (permalink)  
Old 01-06-03, 12:36
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: I have a Subquery (nested) in Access can't figure it out in ASP/vbscript

Quote:
Originally posted by newknew
locations1 and locations2 *are* the subqueries
Yes, in your query they were. In my query they were just aliases for the table locations. I was asking why you don't just use aliases. Using a simple example:

SELECT e1.name empname, e2.name mngrname
FROM emp e1, emp e2
WHERE e1.mngr_no = e2.empno;

i.e. you can use the same table more than once in a query without ambiguity, using aliases (e1 and e2 are aliases for emp).
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #6 (permalink)  
Old 01-06-03, 12:47
newknew newknew is offline
Registered User
 
Join Date: Dec 2002
Posts: 5
Re: I have a Subquery (nested) in Access can't figure it out in ASP/vbscript

Excellent! I tried it out and it works fine. It's nice to learn something in the process...

Thanks,
Tim
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On