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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with subquery question?

Status
Not open for further replies.

spiderbaby

Programmer
Aug 18, 2005
7
US
I need to list sales volume which is the total of all orders by state, my problem is that I can't get the state's orders to total correctly. Here's what I have so far, and it's working with an Access database using VB.NET's built in query builder to do the statement.
The tables look like this:
Customers: cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email
Orders: order_num, order_date, cust_id
OrderItems: order_num, order_item, prod_id, quantity, item_price
Products: prod_id, vend_id, prod_name, prod_price, prod_desc
Vendors: vend_id, vend_name, vend_address, vend_state, vend_zip, vend_country.
This is what I have so far:
SELECT (SELECT cust_state FROM customers WHERE customers.cust_id = orders.cust_id GROUP BY cust_id, cust_state) AS customer_state, (SELECT SUM(item_price*quantity) FROM orderitems WHERE orderitems.order_num = orders.order_num GROUP BY order_num) AS volume
FROM (Customers INNER JOIN Orders ON Customers.cust_id = Orders.cust_id) GROUP BY cust_state, orders.cust_id, orders.order_num

This gives me:
customer_state volume
AZ 1696
IL 189.6
IN 329.6
MI 1648
MI 1867.5

What am I doing wrong? And is there a way to do this without an INNER JOIN? Any help would be greatly appreciated!!!
 
Try this, I haven't tested it:

Code:
Select cust_state, Sum(quantity*item_price) From Customers
Inner Join Orders On Orders.Cust_id = Customers.Cust_id
Inner Join OrderItems OI On OI.Order_num = Orders.Order_num

Group By cust_state
Order By cust_state
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top