我正面臨查詢問題。我的目標是獲得所有產品名稱,質量單位,數量和托盤數量。問題在於托盤的數量。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)' –