Hi Folks,
The SELECT statement at the bottom works good. It retrieves all the cols for every order. I need to improve it to allow the user to say,
"Only show orders where order total > $30000"
I'd guess I start out with something like:
SELECT items.orderID, ( SUM (items.pricequote * items.quantity) ) As OrderTotal others cols....
FROM all-tables
GROUP BY items.orderID and-other-columns
HAVING ( SUM (items.pricequote * items.quantity) ) > 30000
Then having retrieved the order-IDs that are >30000, how would I combine with big SQL below? Would a simple nested SQL do the trick? (ie: I have a list of order ID, and I want all the columns for each ID.)
PS: SQL PHILOSOPHY Q: Re: GROUP BY clause.
Any idea why you have to list ALL the non-aggregated columns in the GROUP BY? In this case, the order ID is a universal identifier, so I could simply say Group by Order ID, and that's enough. That would be enough instructions for a human to do the job, why not SQL? (Grouping by Item Description and seems silly, and a waste of CPU cycles.)
Thanks for any help.
Milt.
(Working all weekend on development...what a geek!)
== Good SQL ===
SELECT offices.state, offices.city, offices.address, offices.postalcode, offices.officeID,
salesreps.repID, salesreps.`last`, salesreps.`first`,
customers.creditrank, customers.repID, customers.address, customers.postalcode,
customers.city, customers.state, customers.custID, customers.customName, orders.orderID,
items.itemcode, items.description, items.pricequote, items.quantity,
orders.shipByDate
FROM customers, orders, items, offices, salesreps
WHERE customers.custID = orders.custID
AND orders.orderID = items.orderID
AND salesreps.repID = customers.repID
AND offices.officeID = salesreps.officeID
ORDER BY offices.officeID, salesreps.repID, customers.customName, orders.orderID
=====
The SELECT statement at the bottom works good. It retrieves all the cols for every order. I need to improve it to allow the user to say,
"Only show orders where order total > $30000"
I'd guess I start out with something like:
SELECT items.orderID, ( SUM (items.pricequote * items.quantity) ) As OrderTotal others cols....
FROM all-tables
GROUP BY items.orderID and-other-columns
HAVING ( SUM (items.pricequote * items.quantity) ) > 30000
Then having retrieved the order-IDs that are >30000, how would I combine with big SQL below? Would a simple nested SQL do the trick? (ie: I have a list of order ID, and I want all the columns for each ID.)
PS: SQL PHILOSOPHY Q: Re: GROUP BY clause.
Any idea why you have to list ALL the non-aggregated columns in the GROUP BY? In this case, the order ID is a universal identifier, so I could simply say Group by Order ID, and that's enough. That would be enough instructions for a human to do the job, why not SQL? (Grouping by Item Description and seems silly, and a waste of CPU cycles.)
Thanks for any help.
Milt.
(Working all weekend on development...what a geek!)
== Good SQL ===
SELECT offices.state, offices.city, offices.address, offices.postalcode, offices.officeID,
salesreps.repID, salesreps.`last`, salesreps.`first`,
customers.creditrank, customers.repID, customers.address, customers.postalcode,
customers.city, customers.state, customers.custID, customers.customName, orders.orderID,
items.itemcode, items.description, items.pricequote, items.quantity,
orders.shipByDate
FROM customers, orders, items, offices, salesreps
WHERE customers.custID = orders.custID
AND orders.orderID = items.orderID
AND salesreps.repID = customers.repID
AND offices.officeID = salesreps.officeID
ORDER BY offices.officeID, salesreps.repID, customers.customName, orders.orderID
=====