A Microsoft Office (Excel, Word) forum. OfficeFrustration

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   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

SQL to get all products ordered together



 
 
Thread Tools Display Modes
  #1  
Old March 20th, 2008, 10:49 PM posted to microsoft.public.access.queries
JRough
external usenet poster
 
Posts: 44
Default SQL to get all products ordered together

I need help with T-SQL syntax. I need all products ordered together.
This would give me all products in an order. I don't want the orders
with only one product.

SELECT o.orderID, o.orderNum, ol.productName
FROM order as o
ON orderline as ol
o.orderID = ol.orderID

How do I get the SQL with 2 or more products in the order?

thanks,
  #2  
Old March 20th, 2008, 11:37 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 2,364
Default SQL to get all products ordered together

One method that should work.

SELECT o.orderID, o.orderNum, ol.productName
FROM order as o INNER JOIN OrderLine as OL
ON o.orderID = ol.orderID
WHERE O.OrderID IN
(SELECT Tmp.OrderID
FROM Order as Tmp INNER JOIN OrderLine as OLA
ON Tmp.OrderID = OLA.OrderID
GROUP BY Tmp.OrderID
HAVING Count(*) 1)



'================================================= ===
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'================================================= ===


JRough wrote:
I need help with T-SQL syntax. I need all products ordered together.
This would give me all products in an order. I don't want the orders
with only one product.

SELECT o.orderID, o.orderNum, ol.productName
FROM order as o
ON orderline as ol
o.orderID = ol.orderID

How do I get the SQL with 2 or more products in the order?

thanks,

  #3  
Old March 21st, 2008, 12:19 AM posted to microsoft.public.access.queries
JRough
external usenet poster
 
Posts: 44
Default SQL to get all products ordered together

So I'm assuming you have to add a tmp table but I don't know why you
created the second alias OLA.
Do you have to create a second alias when you use a separate select
statement? Why can't you use the OL alias?
Is this a correlated sub-query. I have to study this for awhile. it
might take tonight.

On Mar 20, 4:37 pm, John Spencer wrote:
One method that should work.

SELECT o.orderID, o.orderNum, ol.productName
FROM order as o INNER JOIN OrderLine as OL
ON o.orderID = ol.orderID
WHERE O.OrderID IN
(SELECT Tmp.OrderID
FROM Order as Tmp INNER JOIN OrderLine as OLA
ON Tmp.OrderID = OLA.OrderID
GROUP BY Tmp.OrderID
HAVING Count(*) 1)

'================================================= ===
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'================================================= ===

JRough wrote:
I need help with T-SQL syntax. I need all products ordered together.
This would give me all products in an order. I don't want the orders
with only one product.


SELECT o.orderID, o.orderNum, ol.productName
FROM order as o
ON orderline as ol
o.orderID = ol.orderID


How do I get the SQL with 2 or more products in the order?


thanks,


  #4  
Old March 21st, 2008, 12:28 AM posted to microsoft.public.access.queries
JRough
external usenet poster
 
Posts: 44
Default SQL to get all products ordered together

It looks like you just created 2 new aliases for the sub query.
Thanks very much. I think I can figure it out. The answer is the
having clause.
On Mar 20, 4:37 pm, John Spencer wrote:
One method that should work.

SELECT o.orderID, o.orderNum, ol.productName
FROM order as o INNER JOIN OrderLine as OL
ON o.orderID = ol.orderID
WHERE O.OrderID IN
(SELECT Tmp.OrderID
FROM Order as Tmp INNER JOIN OrderLine as OLA
ON Tmp.OrderID = OLA.OrderID
GROUP BY Tmp.OrderID
HAVING Count(*) 1)

'================================================= ===
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'================================================= ===

JRough wrote:
I need help with T-SQL syntax. I need all products ordered together.
This would give me all products in an order. I don't want the orders
with only one product.


SELECT o.orderID, o.orderNum, ol.productName
FROM order as o
ON orderline as ol
o.orderID = ol.orderID


How do I get the SQL with 2 or more products in the order?


thanks,


  #5  
Old March 21st, 2008, 11:14 AM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default SQL to get all products ordered together

Acutally, I could have made that a bit simpler

SELECT o.orderID, o.orderNum, ol.productName
FROM order as o INNER JOIN OrderLine as OL
ON o.orderID = ol.orderID
WHERE O.OrderID IN
(SELECT Tmp.OrderID
FROM OrderLine as Tmp
GROUP BY Tmp.OrderID
HAVING Count(*) 1)


--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..

"JRough" wrote in message
...
So I'm assuming you have to add a tmp table but I don't know why you
created the second alias OLA.
Do you have to create a second alias when you use a separate select
statement? Why can't you use the OL alias?
Is this a correlated sub-query. I have to study this for awhile. it
might take tonight.

On Mar 20, 4:37 pm, John Spencer wrote:
One method that should work.

SELECT o.orderID, o.orderNum, ol.productName
FROM order as o INNER JOIN OrderLine as OL
ON o.orderID = ol.orderID
WHERE O.OrderID IN
(SELECT Tmp.OrderID
FROM Order as Tmp INNER JOIN OrderLine as OLA
ON Tmp.OrderID = OLA.OrderID
GROUP BY Tmp.OrderID
HAVING Count(*) 1)

'================================================= ===
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'================================================= ===

JRough wrote:
I need help with T-SQL syntax. I need all products ordered together.
This would give me all products in an order. I don't want the orders
with only one product.


SELECT o.orderID, o.orderNum, ol.productName
FROM order as o
ON orderline as ol
o.orderID = ol.orderID


How do I get the SQL with 2 or more products in the order?


thanks,




 




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:58 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.