如何将 2 个具有不同 where 子句的 oracle 查询合并为 1 个查询
How to combine 2 oracle queries with differrent where clauseses into 1 query
我有以下 2 个具有不同 where 条件的 oracle 查询
(条件相同的某些条件)。是否可以将这 2 个查询合并为 1 个查询?
查询在这里
不同条件:TX_TYPE, TX_STATUS
常见条件:SUBSCRIBER_ID, MERCHANT_ID, IS_TAXABLE, TX_DATE
查询 1:
select sum(t.AMOUNT) Amt1
from RS_TRANSACTION_LOG t, RS_MERCHANT m
where t.SUBSCRIBER_ID = 11338329
and t.MERCHANT_ID = m.MERCHANT_ID
and t.TX_TYPE = '1'
and t.TX_STATUS in (5, 42)
and m.IS_TAXABLE = 'Y'
and TX_DATE between to_date('11/01/2015 00:00:00','MM/dd/YYYY HH24:MI:SS')
and to_date('11/30/2015 23:59:59','MM/dd/YYYY HH24:MI:SS');
查询 2:
select sum(t.AMOUNT) Amt2
from RS_TRANSACTION_LOG t, RS_MERCHANT m
where t.SUBSCRIBER_ID = 11338329
and t.MERCHANT_ID = m.MERCHANT_ID
and t.TX_TYPE = '5'
and t.TX_STATUS = 5
and m.IS_TAXABLE = 'Y'
and TX_DATE between to_date('11/01/2015 00:00:00','MM/dd/YYYY HH24:MI:SS')
and to_date('11/30/2015 23:59:59','MM/dd/YYYY HH24:MI:SS');
如何使用 OR
组合条件和 CASE
分隔金额:
select sum(case when t.TX_TYPE = '1' and t.TX_STATUS in (5,42) then t.AMOUNT else 0 end) Amt1,
sum(case when t.TX_TYPE = '5' and t.TX_STATUS = 5 then t.AMOUNT else 0 end) Amt2
from RS_TRANSACTION_LOG t,
RS_MERCHANT m
where t.SUBSCRIBER_ID=11338329
and t.MERCHANT_ID=m.MERCHANT_ID
and ((t.TX_TYPE = '1' and t.TX_STATUS in (5,42)) or
(t.TX_TYPE='5' and t.TX_STATUS = '5'))
and m.IS_TAXABLE='Y'
and TX_DATE between
to_date('11/01/2015 00:00:00','MM/dd/YYYY HH24:MI:SS') and
to_date('11/30/2015 23:59:59','MM/dd/YYYY HH24:MI:SS');
我有以下 2 个具有不同 where 条件的 oracle 查询 (条件相同的某些条件)。是否可以将这 2 个查询合并为 1 个查询? 查询在这里
不同条件:TX_TYPE, TX_STATUS
常见条件:SUBSCRIBER_ID, MERCHANT_ID, IS_TAXABLE, TX_DATE
查询 1:
select sum(t.AMOUNT) Amt1
from RS_TRANSACTION_LOG t, RS_MERCHANT m
where t.SUBSCRIBER_ID = 11338329
and t.MERCHANT_ID = m.MERCHANT_ID
and t.TX_TYPE = '1'
and t.TX_STATUS in (5, 42)
and m.IS_TAXABLE = 'Y'
and TX_DATE between to_date('11/01/2015 00:00:00','MM/dd/YYYY HH24:MI:SS')
and to_date('11/30/2015 23:59:59','MM/dd/YYYY HH24:MI:SS');
查询 2:
select sum(t.AMOUNT) Amt2
from RS_TRANSACTION_LOG t, RS_MERCHANT m
where t.SUBSCRIBER_ID = 11338329
and t.MERCHANT_ID = m.MERCHANT_ID
and t.TX_TYPE = '5'
and t.TX_STATUS = 5
and m.IS_TAXABLE = 'Y'
and TX_DATE between to_date('11/01/2015 00:00:00','MM/dd/YYYY HH24:MI:SS')
and to_date('11/30/2015 23:59:59','MM/dd/YYYY HH24:MI:SS');
如何使用 OR
组合条件和 CASE
分隔金额:
select sum(case when t.TX_TYPE = '1' and t.TX_STATUS in (5,42) then t.AMOUNT else 0 end) Amt1,
sum(case when t.TX_TYPE = '5' and t.TX_STATUS = 5 then t.AMOUNT else 0 end) Amt2
from RS_TRANSACTION_LOG t,
RS_MERCHANT m
where t.SUBSCRIBER_ID=11338329
and t.MERCHANT_ID=m.MERCHANT_ID
and ((t.TX_TYPE = '1' and t.TX_STATUS in (5,42)) or
(t.TX_TYPE='5' and t.TX_STATUS = '5'))
and m.IS_TAXABLE='Y'
and TX_DATE between
to_date('11/01/2015 00:00:00','MM/dd/YYYY HH24:MI:SS') and
to_date('11/30/2015 23:59:59','MM/dd/YYYY HH24:MI:SS');