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

Customer Total?

Status
Not open for further replies.

milton747

Programmer
Apr 21, 2005
133
US
Hi Folks,

The CustomerTotal is always the same as the GrandTotal.
I thought my syntax would provide the total for ALL orders for each Customer ID.
Any suggestions?

Thank U.

TABLES.....

Table_Orders
* OrderID
* CustID

Table_Customers
* CustID

Table_Items
* OrderID
* itemcode
* quantity
* price

---------------------------

SELECT
customers.custID, orders.orderID,
items.quantity, items.pricequote, items.itemcode,
items.orderID, ( items.quantity * items.pricequote ) AS LineTotal,

(SELECT SUM ( items.quantity * items.pricequote )
FROM items
WHERE customers.custID = orders.custID
GROUP BY customers.custID
) AS CustomerTotal,

(SELECT SUM ( items.quantity * items.pricequote )
FROM items
) AS GrandTotal

FROM customers, orders, items
WHERE customers.custID = orders.custID
AND orders.orderID = items.orderID
AND customers.custID < :pCID

ORDER BY customers.custID, orders.orderID, 7 DESC

-- end --
 
Perhaps:

[tt]SELECT orders.custid,
orders.orderid,
items.quantity,
items.pricequote,
items.itemcode,
([items].[quantity] * [items].[pricequote]) AS linetotal,
ct.custlinetotal,
gt.grandtotal
FROM (SELECT SUM(items.quantity * items.pricequote) AS grandtotal
FROM items) AS gt,
items
INNER JOIN (orders
INNER JOIN (SELECT orders.custid,
SUM((items.[quantity] * items.[pricequote])) AS custlinetotal
FROM orders
INNER JOIN items
ON orders.orderid = items.orderid
GROUP BY orders.custid) AS ct
ON orders.custid = ct.custid)
ON items.orderid = orders.orderid[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top