簡體   English   中英

Oracle-計數條件案例

[英]Oracle - Count condition case

我面臨查詢問題。 我的目標是獲取所有產品名稱,質量單位,數量和所位於的托盤數。問題在於項目托盤數。

查詢:

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(*)中做一些條件(這是我認為應該做的)。 在表wms_stock我得到了product_idcontainer_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 ContainerId || ProductId)

僅供參考, || 不是運算符,而是Oracle中的串聯運算符。 因此,我只是將這兩列都連接在一起,並從中選擇了它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM