hessodreamy
Programmer
I'm trying to do a query to get sales totals. Here's the definitions:
tOrders has the delivery prices, and tOrderProds has item costs, but naturally when I do a join between the tables, the delivery costs for the whole order appear in every row, so when I do a sum, I end up with the wrong figures. Here's the query I've been using:
Any ideas how I stop the query from adding delivery charges twice?
Code:
CREATE TABLE `torders` (
`orderID` int(11) unsigned NOT NULL auto_increment,
`uts` int(11) NOT NULL default '0',
`subTotal` float NOT NULL default '0',
`delivery` float NOT NULL default '0',
`deliveryCost` float NOT NULL default '0',
) ;
CREATE TABLE `torderpos` (
`purchaseOrder` int(11) unsigned NOT NULL auto_increment,
`orderID` int(11) unsigned NOT NULL default '0',
)
CREATE TABLE `torderprods` (
`purchaseOrder` int(11) unsigned NOT NULL default '0',
`productID` int(11) unsigned NOT NULL default '0',
`prodQty` int(11) NOT NULL default '0',
`salePrice` float NOT NULL default '0',
`costPrice` float NOT NULL default '0',
)
tOrders has the delivery prices, and tOrderProds has item costs, but naturally when I do a join between the tables, the delivery costs for the whole order appear in every row, so when I do a sum, I end up with the wrong figures. Here's the query I've been using:
Code:
SELECT sum(costPrice*prodQty)+sum(deliveryCost) as cost
from tOrders o
INNER JOIN tOrderPOs pos ON o.orderId=pos.orderID
INNER JOIN tOrderProds op ON pos.purchaseOrder=op.purchaseOrder
WHERE from_unixtime(uts,'%Y-%m-%d')='2005-09-08'
GROUP BY o.orderid ORDER BY uts;