SQL newbie
am 16.08.2004 11:10:14 von David McCallumI have a query which returns the correct resultset:
SELECT invoicedetail.itemtype_id, itemtype.itemname,
AVG(CASE WHEN invoice.isinvoice THEN invoicedetail.totalitemcost END) AS
averagecost,
COUNT(CASE WHEN invoice.isinvoice THEN itemtype.itemname END) AS numsales,
COUNT(CASE WHEN NOT invoice.isinvoice THEN itemtype.itemname END) AS
numcredits,
SUM(CASE WHEN invoice.isinvoice THEN invoicedetail.totalitemcost ELSE 0.00
END) AS totalcost,
SUM(CASE WHEN NOT invoice.isinvoice THEN invoicedetail.totalitemcost ELSE
0.00 END) AS totalcredit
FROM itemtype, invoicedetail, invoice
WHERE itemtype.itemtype_id=invoicedetail.itemtype_id
AND invoicedetail.invoice_id=invoice.invoice_id
AND itemtype.itemtype_id=1
GROUP BY invoicedetail.itemtype_id, itemtype.itemname
How every when I change it to this:
SELECT invoicedetail.itemtype_id, itemtype.itemname,
AVG(CASE WHEN invoice.isinvoice THEN invoicedetail.totalitemcost END) AS
averagecost,
COUNT(CASE WHEN invoice.isinvoice THEN itemtype.itemname END) AS numsales,
COUNT(CASE WHEN NOT invoice.isinvoice THEN itemtype.itemname END) AS
numcredits,
SUM(CASE WHEN invoice.isinvoice THEN invoicedetail.totalitemcost ELSE 0.00
END) AS totalcost,
SUM(CASE WHEN NOT invoice.isinvoice THEN invoicedetail.totalitemcost ELSE
0.00 END) AS totalcredit,
COUNT(inventory.itemtype_id) AS totalstock **** CHANGED LINE
FROM itemtype, invoicedetail, invoice, inventory **** CHANGED LINE
WHERE itemtype.itemtype_id=invoicedetail.itemtype_id
AND invoicedetail.invoice_id=invoice.invoice_id
AND invoicedetail.itemtype_id=inventory.itemtype_id **** CHANGED LINE
AND itemtype.itemtype_id=1
GROUP BY invoicedetail.itemtype_id, itemtype.itemname
The original columns are multiplied by 8000, coincedently the number of
items in inventory that have itemtype_id=1.
Basically what I want is the original columns plus the number of items in
inventory
Can anyone help
TIA
David McCallum