我需要找到具有最大利润率的股票项目,但我不确定如何这样做,因为这是在查询中计算而不是从表中获取。 我的脚本如下(使用Oracle 11g),当我只需要一个时,它会返回几个库存项。请帮忙
SELECT (UPPER(Orderline.StockID) || ' ' || INITCAP(StockDesc)) AS "Item",
CategoryDesc AS "Category",
LocationCity AS "Warehouse",
Quantity AS "Number Sold",
TO_CHAR(Quantity * UnitPrice) AS "Total Income",
TO_CHAR(Quantity * UnitCost) AS "Total Cost",
TO_CHAR((Quantity * UnitPrice) - (Quantity * UnitCost)) AS "Profit"
FROM Orderline INNER JOIN
(Stock INNER JOIN
("CATEGORY" INNER JOIN Warehouse
ON "CATEGORY".Warehouse = Warehouse.WarehouseID)
ON Stock.ItemCategory = "CATEGORY".CategoryID)
ON Orderline.StockID = Stock.StockNo
ORDER BY ((Quantity * UnitPrice) - (Quantity * UnitCost)) DESC;
答案 0 :(得分:0)
试试这个......
select * from (SELECT (UPPER(Orderline.StockID) || ' ' || INITCAP(StockDesc)) AS "Item",
CategoryDesc AS "Category",
LocationCity AS "Warehouse",
Quantity AS "Number Sold",
TO_CHAR(Quantity * UnitPrice) AS "Total Income",
TO_CHAR(Quantity * UnitCost) AS "Total Cost",
TO_CHAR((Quantity * UnitPrice) - (Quantity * UnitCost)) AS "Profit"
FROM Orderline INNER JOIN
(Stock INNER JOIN
("CATEGORY" INNER JOIN Warehouse
ON "CATEGORY".Warehouse = Warehouse.WarehouseID)
ON Stock.ItemCategory = "CATEGORY".CategoryID)
ON Orderline.StockID = Stock.StockNo
ORDER BY Profit DESC)
where rownum = 1;
我在评论中看到您已在内部选择中尝试rownum=1
...这将选择唯一Item
并将其与order by
排序。对一组元素进行排序的方法并不多。但是,如果您已经对所有元素进行了排序,那么您可以选择第一个元素。这就是这里发生的事情。
查看有关此案例here的更多信息。