我从podetail表中得到以下查询,其中我计算了"一个"的平均价格。一次唯一的项目。我需要创建一个" avgUnitCost"的列表。有没有办法在子查询中使用这个现有查询来生成avgUnitCost列表?
SELECT items.`itemName`,
SUM(podetails.`qtyReceived`) AS qtyRcvd,
SUM(podetails.`qtyReceived` * podetails.`unitCost`) AS totalUnitCost,
SUM(podetails.`qtyReceived` * podetails.`unitCost`) / SUM(podetails.`qtyReceived`) AS avgUnitCost
FROM podetails, items, purchaseorders
WHERE purchaseorders.`poID` = podetails.`poID`
AND podetails.`itemID` = items.`itemID`
AND podetails.`itemID` = 3;>
答案 0 :(得分:0)
您在此处缺少GROUP BY
声明。不确定哪一列是准确的,但假设您应该按items.itemName
分组
SELECT items.`itemName`,
SUM(podetails.`qtyReceived`) AS qtyRcvd,
SUM(podetails.`qtyReceived` * podetails.`unitCost`) AS totalUnitCost,
SUM(podetails.`qtyReceived` * podetails.`unitCost`) / SUM(podetails.`qtyReceived`) AS avgUnitCost
FROM podetails JOIN purchaseorders
ON
purchaseorders.`poID` = podetails.`poID`
JOIN items
ON
podetails.`itemID` = items.`itemID`
WHERE
podetails.`itemID` = 3
GROUP BY items.`itemName`;
答案 1 :(得分:0)
如果您希望avgUnitCost
显示purchaseorders
表格中所有项目的SELECT items.`itemName`,
SUM(podetails.`qtyReceived`) AS qtyRcvd,
SUM(podetails.`qtyReceived` * podetails.`unitCost`) AS totalUnitCost,
SUM(podetails.`qtyReceived` * podetails.`unitCost`) / SUM(podetails.`qtyReceived`) AS avgUnitCost
FROM podetails, items, purchaseorders
WHERE purchaseorders.`poID` = podetails.`poID`
AND podetails.`itemID` = items.`itemID`
GROUP BY items.`itemID`;
,则以下查询可能会对您有所帮助
GROUP BY
使用while (iterator.hasNext()) {
String key = (String) iterator.next();
Iist value = (list)map.get(key);
System.out.println("Key: "+ key+", Value: "+ value);
}
的这一小改动可能会为您提供预期的结果