Hi everyone,
I am going crazy trying to nest subqueries to get simple sub-totals. The goal is a row like this:
STATE | CUSTID | CUSTNAME | ORDERID | STATE TOTAL | CUSTOMER TOTAL | ORDER TOTAL
I'm trying to wrap INNER JOINS around my core SQL (which returns ORDER TOTAL) to get the other 2 totals, for CUSTOMER and STATE. Simple tables shown below. But I can't seem to stack the inner joins correctly. Losing my hair.
Any help welcome.
Thanks. Milt.
-------------------------------
== customer table ==
- state
- custID
- custname
== order table ==
- orderID
- custID
== items ==
- orderID
- price
- quantity
- Below works to return order total.
SELECT orders.custID, orders.orderID, SSA.OT
FROM orders
INNER JOIN
(SELECT
items.orderID,
SUM ( items.quantity * items.pricequote ) AS OT
FROM items
GROUP BY items.orderID ) SSA
ON SSA.orderID = orders.orderID
I am going crazy trying to nest subqueries to get simple sub-totals. The goal is a row like this:
STATE | CUSTID | CUSTNAME | ORDERID | STATE TOTAL | CUSTOMER TOTAL | ORDER TOTAL
I'm trying to wrap INNER JOINS around my core SQL (which returns ORDER TOTAL) to get the other 2 totals, for CUSTOMER and STATE. Simple tables shown below. But I can't seem to stack the inner joins correctly. Losing my hair.
Any help welcome.
Thanks. Milt.
-------------------------------
== customer table ==
- state
- custID
- custname
== order table ==
- orderID
- custID
== items ==
- orderID
- price
- quantity
- Below works to return order total.
SELECT orders.custID, orders.orderID, SSA.OT
FROM orders
INNER JOIN
(SELECT
items.orderID,
SUM ( items.quantity * items.pricequote ) AS OT
FROM items
GROUP BY items.orderID ) SSA
ON SSA.orderID = orders.orderID