排除一列中多个结果中两个结果的负数

时间:2019-07-09 14:51:20

标签: sql oracle

我正在运行一个查询,该查询会导致交易的负数和正数。我希望它们都针对二手产品展示;但是,我只想对交货和转移表示肯定。

所有使用,交付和转移都将从“交易类型”列中提取。我不知道将正数仅限制在交付和转移交易类型中,并保留正数和负数以供使用的最佳方法。

我试图在where子句中对此进行限制,但要么将其限制为仅严格交付和转移,要么仅对所有交易类型都提取正数。

在下面的代码中,它显示了我尝试执行的case语句,但是,在尝试运行脚本时抛出了错误。

SELECT distinct 
  case when NOT upper(transaction_type) = 'PURCHASE' 
            AND DST_PROPERTY_TYPE = 'WAREHOUSE' THEN 'Return' 
       ELSE transaction_type END transaction_type
  , transaction_type as transaction_type_OG --, division_name2
  , case when transaction_type in ('Delivery','Transfer') 
         then quantity >=0 else quantity end
  , transaction_id
  , comments
  , src_division_name
  , src_property_name
  , src_property_type
  , src_afe, src_rig_name
  , dst_division_id
  , dst_division_name
  , dst_property_name
  , dst_property_type
  , dst_afe
  , dst_rig_name
  , M.CATEGORY_NAME
  , item_id
  , item_name
  , quantity
  , created_by, ticket_number
FROM transactions.tables m
WHERE not CATEGORY_ID IN ('1294','1295','1337') 
  AND MASTER_PROPERTY NOT LIKE '%products%' 
  AND MASTER_PROPERTY NOT LIKE '%TEST%' 
  and transaction_dt = trunc(sysdate) - 1
order by transaction_type, src_property_type, src_property_name

2 个答案:

答案 0 :(得分:0)

怎么样?

with data(trans_type, qty) as (
  select 'Used',-1 from dual union all
  select 'Delivery',-1 from dual union all
  select 'Transfer',-1 from dual union all
  select 'Used',1 from dual union all
  select 'Delivery',1 from dual union all
  select 'Transfer',1 from dual
)
select * from data
where (trans_type = 'Used' or qty > 0);

TRANS_TY        QTY
-------- ----------
Used             -1
Used              1
Delivery          1
Transfer          1

答案 1 :(得分:0)

更新的案例声明:

case when transaction_type in ('Delivery','Transfer') and quantity<0 then 'Duplicate' else transaction_type end TRANSACTION_TYPE_USE

然后将此脚本添加到我的水晶报表中后,我便过滤掉了重复项。

谢谢!