加入不起作用的条件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加入不起作用的条件相关的知识,希望对你有一定的参考价值。
我有条件在哪里条件我试图根据某些条件加入不同字段的表。但它不允许。是否有另一种方法来获得结果。你们任何人都可以帮我解决这个问题。
select
s1.Seq1,
s1.Seq2,
s1.Transf_Date,
s1.Place_Name,
s1.Place_Code,
'0' as Completion_Flag,
s1.Flag
from
(
select
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ else T.PO_SEQ end as Seq1,
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ2 else T.PO_SEQ2 end as Seq2,
case when T.STOCK_TRANSF_SEQ > 0 then '0' else '1' end as Flag,
T.STOCK_TRANSF_DATE as Transf_Date,
min(T.WHIN_PLACE_CD) as Place_Code,
min(C.CUSTOMER_NAME1) as Place_Name
from
T_TRANSFER_ORDER t
join M_CUSTOMER c ON t.WHIN_PLACE_CD = c.CUSTOMER_CODE
and C.PLACE_F = '1'
and C.DELETE_FLAG = 0
and C.OWN_WH = '1'
and C.WAREHOUSE_CODE = '10'
join M_CUSTOMER C2 on T.WHOUT_PLACE_CD = C2.CUSTOMER_CODE
where (T.STOCK_TRANSF_SEQ > 0 or T.PO_SEQ > 0)
and T.WHOUT_PLACE_CD = '002001000'
group by T.STOCK_TRANSF_SEQ, T.STOCK_TRANSF_SEQ2, t.PO_SEQ,T.PO_SEQ2, t.STOCK_TRANSF_DATE
having min(t.RG_FIN_F) = 0
) s1
join
(
select res.Seq1,
res.Seq2,
case when res.CNT= res.CNT2 then 1 else 0 end AS Completion_Flag
from (
select
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ else T.PO_SEQ end as Seq1,
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ2 else T.PO_SEQ2 end as Seq2,
count(*) CNT,
(select count(DISTINCT ITEM_CD) from T_TRANSFER_RESULT
where case when T.STOCK_TRANSF_SEQ > 0 then (STOCK_TRANSF_SEQ = T.STOCK_TRANSF_SEQ
AND STOCK_TRANSF_SEQ2 = T.STOCK_TRANSF_SEQ2) ELSE( PO_SEQ = T.PO_SEQ AND PO_SEQ2 = T.PO_SEQ2) end
and OUTPUT_FLAG = '1' AND DELETE_FLAG = 0) as CNT2
from
T_TRANSFER_ORDER t
group BY t.STOCK_TRANSF_SEQ, t.STOCK_TRANSF_SEQ2, t.PO_SEQ, t.PO_SEQ2
) res
)
s2 on s1.Seq1 = s2.Seq1 and s1.Seq2 = s2.Seq2 and s2.Completion_Flag = 0
order BY s1.Transf_Date
答案
我改变了它的工作原理结构
select
s1.Seq1,
s1.Seq2,
s1.Transf_Date,
s1.Place_Name,
s1.Place_Code,
'0' as Completion_Flag,
s1.Flag
from
(
select
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ else T.PO_SEQ end as Seq1,
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ2 else T.PO_SEQ2 end as Seq2,
case when T.STOCK_TRANSF_SEQ > 0 then '0' else '1' end as Flag,
T.STOCK_TRANSF_DATE as Transf_Date,
min(T.WHIN_PLACE_CD) as Place_Code,
min(C.CUSTOMER_NAME1) as Place_Name
from
T_TRANSFER_ORDER t
join M_CUSTOMER c ON t.WHIN_PLACE_CD = c.CUSTOMER_CODE
and C.PLACE_F = '1'
and C.DELETE_FLAG = 0
and C.OWN_WH = '1'
and C.WAREHOUSE_CODE = '10'
join M_CUSTOMER C2 on T.WHOUT_PLACE_CD = C2.CUSTOMER_CODE
where (T.STOCK_TRANSF_SEQ > 0 or T.PO_SEQ > 0)
and T.WHOUT_PLACE_CD = '002001000'
group by T.STOCK_TRANSF_SEQ, T.STOCK_TRANSF_SEQ2, t.PO_SEQ,T.PO_SEQ2, t.STOCK_TRANSF_DATE
having min(t.RG_FIN_F) = 0
) s1
join
(
select res.Seq1,
res.Seq2,
case when res.CNT= res.CNT2 then 1 else 0 end AS Completion_Flag
from (
select
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ else T.PO_SEQ end as Seq1,
case when T.STOCK_TRANSF_SEQ > 0 then T.STOCK_TRANSF_SEQ2 else T.PO_SEQ2 end as Seq2,
count(*) CNT,
(select count(DISTINCT ITEM_CD) from T_TRANSFER_RESULT
where ((T.STOCK_TRANSF_SEQ > 0 AND STOCK_TRANSF_SEQ = T.STOCK_TRANSF_SEQ
AND STOCK_TRANSF_SEQ2 = T.STOCK_TRANSF_SEQ2 )
OR (T.STOCK_TRANSF_SEQ = 0 AND PO_SEQ = T.PO_SEQ AND PO_SEQ2 = T.PO_SEQ2))
and OUTPUT_FLAG = '1' AND DELETE_FLAG = 0) as CNT2
from
T_TRANSFER_ORDER t
group BY t.STOCK_TRANSF_SEQ, t.STOCK_TRANSF_SEQ2, t.PO_SEQ, t.PO_SEQ2
) res
)
s2 on s1.Seq1 = s2.Seq1 and s1.Seq2 = s2.Seq2 and s2.Completion_Flag = 0
order BY s1.Transf_Date
另一答案
问题似乎是在WHERE谓词中使用CASE,如下所示。
select count(DISTINCT ITEM_CD)
from T_TRANSFER_RESULT
where case when T.STOCK_TRANSF_SEQ > 0
then (STOCK_TRANSF_SEQ = T.STOCK_TRANSF_SEQ
AND STOCK_TRANSF_SEQ2 = T.STOCK_TRANSF_SEQ2)
ELSE (PO_SEQ = T.PO_SEQ AND PO_SEQ2 = T.PO_SEQ2) end
and OUTPUT_FLAG = '1' AND DELETE_FLAG = 0) as CNT2;
select * from mytab;
输出:
A B
1 Balaji
2 Vanchi
方式不正确:
select *
from mytab
where case when a = 1 then b = 'Balaji' else b = 'Vanchi' end and a > 0;
输出:
ORA-00905: missing keyword
正确的方式:
select * from mytab where b = (case when a = 1 then 'Balaji' else 'Vanchi' end) and a > 0;
输出:
A B
1 Balaji
2 Vanchi
以上是关于加入不起作用的条件的主要内容,如果未能解决你的问题,请参考以下文章