我正面临查询问题。我的目标是获得所有产品名称,质量单位,数量和托盘数量。问题在于托盘的数量。Oracle - 计数条件案例
QUERY:
SELECT "Product_Name"
, "Unit_of_Mass"
, SUM("Quantity_Per_UOM")
, Count(*) as "Number_Of_Pallet"
FROM
(select p.prod_desc as "Product_Name"
, s.quantity as "Quantity_Per_UOM"
, u.description as "Unit_of_Mass"
, s.container_id
, s.product_id
from wms_stock s
join wms_product p on p.product_id = s.product_id
join wms_uom u on p.uom_base_id = u.uom_id
)
group by "Product_Name", "Unit_of_Mass"
它几乎工作。问题是我需要在Count(*)中做一些条件(这是我认为应该完成的)。在表我得到了product_id
和container_id
,当它们在某一行时它们是相同的,它应该将托盘数量作为1
,但仍然会增加数量。
所以从第一选择:
Product_Name | Quantity | UnitOfMass | ContainerId | ProductId
A | 2 | kg | 10 | 11
A | 1 | kg | 10 | 11
B | 2 | kg | 11 | 12
我应该可以导致
Product_Name | Quantity_Per_UOM | UnitOfMass | Number_Of_Pallet
A | 3 | kg | 1
B | 2 | kg | 1
您可以尝试'COUNT(DISTINCT Number_Of_Pallet).' –
这样做使得Number_Of_Pallet中的结果为1.也许这不是数量和条件的问题。 –
试试这个 - “COUNT(DISTINCT ContainerId || ProductId)' –