I am getting and calculating some basic order information in my SQL query. I have it working as it should but have been reading about the GROUP BY SQL Clause. I am wondering if the following SQL statement would benefit from GROUP BY and if it would be more efficient to use it? Thanks!
SELECT orders.billerID,
orders.invoiceDate,
orders.txnID,
orders.bName,
orders.bStreet1,
orders.bStreet2,
orders.bCity,
orders.bState,
orders.bZip,
orders.bCountry,
orders.sName,
orders.sStreet1,
orders.sStreet2,
orders.sCity,
orders.sState,
orders.sZip,
orders.sCountry,
orders.paymentType,
orders.invoiceNotes,
orders.pFee,
orders.shipping,
orders.tax,
orders.reasonCode,
orders.txnType,
orders.customerID,
customers.firstName AS firstName,
customers.lastName AS lastName,
customers.businessName AS businessName,
orderStatus.statusName AS orderStatus,
(IFNULL(SUM((orderItems.itemPrice * orderItems.itemQuantity)), 0.00) + orders.shipping + orders.tax) AS orderTotal,
((IFNULL(SUM((orderItems.itemPrice * orderItems.itemQuantity)), 0.00) + orders.shipping + orders.tax) - (SELECT IFNULL(SUM(payments.amount), 0.00) FROM payments WHERE payments.orderID = orders.id)) AS orderBalance
FROM orders
LEFT JOIN customers ON orders.customerID = customers.id
LEFT JOIN orderStatus ON orders.orderStatus = orderStatus.id
LEFT JOIN orderItems ON orderItems.orderID = orders.id
LEFT JOIN payments ON payments.orderID = orders.id
GROUP BYwould probably allow the SQL engine to better optimize your query but would make it harder to read due to the large number of grouping parameters.Another option as recommended by the SQL Team is to consider using Sub queries. This can often make the
GROUP BYstatements much simpler and makes the overall query much easier to read.Using a Sub query:
Using a
GROUP BY:GROUP BYExplained:You can thing of
GROUP BYas collecting records together that have similar data. For my example I am going to use a simple produce table withCategory,NameandPricecolumns. If I group the data byCategoryI can aggregate ( i.e.SUM,COUNT,MIN,MAX, etc.) based on any of the other columns. Since I am grouping by theCategorycolumn the resulting records will have a unique value forCategory. Any of the other columns might be return different value and therefore cannot be included in the select statement.Name, Category, Price
Green Peppers, Peppers, 1.50
Orange Peppers, Peppers, 2.50
Yellow Peppers, Peppers, 2.50
Lemons, Citrus, 1.00
Oranges, Citrus, 1.00
Limes, Citrus, 1.00