Hi folks,
I added a clause to a previously working SQL. (Provided kindly form this forum)
(See below for complete good and bad verisons. )
The good version inserts the OrderTotal in each row:
(SELECT SUM ( items.quantity * items.pricequote )
FROM items
WHERE items.orderID = orders.orderID
AND customers.custID = orders.custID
)
AS OrderTotal,
That worls good. So I inserted a cluase to get the total for all orders for each customer. As follows:
(SELECT SUM ( items.quantity * items.pricequote )
FROM items
WHERE items.orderID = orders.orderID
AND customers.custID = orders.custID
GROUP BY customers.custID
)
AS CustomerTotal,
It runs, however, CustomerTotal is the same as OrderTotal.
Any suggestions how to get it to show the customer total.
And while I'm asking, how about a GrandTotal for all rows? (So I can do % calculations)
Big thanks
Milton.
Full SQL Below...
** THIS WORKS ---------------------- begin
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 items.orderID = orders.orderID
AND customers.custID = orders.custID
)
AS OrderTotal,
LineTotal / OrderTotal * 100 AS Percentage
FROM customers, orders, items
WHERE customers.custID = orders.custID AND orders.orderID = items.orderID
AND customers.custID <
CID
ORDER BY
customers.custID,
orders.orderID,
7 DESC
** THIS WORKS -------- End
** THIS RUNS -- But wrong anwer -------- begin
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 items.orderID = orders.orderID
AND customers.custID = orders.custID
)
AS OrderTotal,
(SELECT SUM ( items.quantity * items.pricequote )
FROM items
WHERE items.orderID = orders.orderID
AND customers.custID = orders.custID
GROUP BY customers.custID
)
AS CustomerTotal,
LineTotal / OrderTotal * 100 AS Percentage
FROM customers, orders, items
WHERE customers.custID = orders.custID AND orders.orderID = items.orderID
AND customers.custID <
CID
ORDER BY
customers.custID,
orders.orderID,
7 DESC
--- End
I added a clause to a previously working SQL. (Provided kindly form this forum)
(See below for complete good and bad verisons. )
The good version inserts the OrderTotal in each row:
(SELECT SUM ( items.quantity * items.pricequote )
FROM items
WHERE items.orderID = orders.orderID
AND customers.custID = orders.custID
)
AS OrderTotal,
That worls good. So I inserted a cluase to get the total for all orders for each customer. As follows:
(SELECT SUM ( items.quantity * items.pricequote )
FROM items
WHERE items.orderID = orders.orderID
AND customers.custID = orders.custID
GROUP BY customers.custID
)
AS CustomerTotal,
It runs, however, CustomerTotal is the same as OrderTotal.
Any suggestions how to get it to show the customer total.
And while I'm asking, how about a GrandTotal for all rows? (So I can do % calculations)
Big thanks
Milton.
Full SQL Below...
** THIS WORKS ---------------------- begin
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 items.orderID = orders.orderID
AND customers.custID = orders.custID
)
AS OrderTotal,
LineTotal / OrderTotal * 100 AS Percentage
FROM customers, orders, items
WHERE customers.custID = orders.custID AND orders.orderID = items.orderID
AND customers.custID <
ORDER BY
customers.custID,
orders.orderID,
7 DESC
** THIS WORKS -------- End
** THIS RUNS -- But wrong anwer -------- begin
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 items.orderID = orders.orderID
AND customers.custID = orders.custID
)
AS OrderTotal,
(SELECT SUM ( items.quantity * items.pricequote )
FROM items
WHERE items.orderID = orders.orderID
AND customers.custID = orders.custID
GROUP BY customers.custID
)
AS CustomerTotal,
LineTotal / OrderTotal * 100 AS Percentage
FROM customers, orders, items
WHERE customers.custID = orders.custID AND orders.orderID = items.orderID
AND customers.custID <
ORDER BY
customers.custID,
orders.orderID,
7 DESC
--- End