好的,这是问题所在。
我有两个表,t1
和t2
。使用两个字段:
我想找到一个期间(AP = 202,FY = 2017)的产品POSTED_TRAN_AMT
和BEG_AUM_AMT
不同。
目前我被困在这里:
select PRODUCT, sum(POSTED_TRAN_AMT) from t1
where FISCAL_YEAR = 2017 and ACCOUNTING_PERIOD = 202 and ACCOUNT = '995050'
group by product
union all
select PRODUCT sum(_BEG_AUM_AMT) from t2
where FISCAL_YEAR = 2017 and ACCOUNTING_PERIOD = 202 group by product
注意:手动excel搜索后,优秀产品(值为0)出现在t2
中答案 0 :(得分:0)
将它们写为子查询并加入
with t1 as (
select PRODUCT,
sum(POSTED_TRAN_AMT) as POSTED_TRAN_AMT
from t1
where FISCAL_YEAR = 2017
and ACCOUNTING_PERIOD = 202
and ACCOUNT = '995050'
group by product
) , t2 as (
select PRODUCT,
sum(BEG_AUM_AMT) as BEG_AUM_AMT
from t2
where FISCAL_YEAR = 2017
and ACCOUNTING_PERIOD = 202
group by product
)
select t1.product,
t1.POSTED_TRAN_AMT,
t2.BEG_AUM_AMT
from t1
join t2 on t1.product = t2.product
where t1.POSTED_TRAN_AMT != t2.BEG_AUM_AMT
如果差异可能是t1
但不是t2
中的PRODUCT,则需要使用外连接并修改WHERE子句,如下所示:
select t1.product,
t1.POSTED_TRAN_AMT,
t2.BEG_AUM_AMT
from t1
left outer join t2 on t1.product = t2.product
where ( t2.product is null
or t1.POSTED_TRAN_AMT != t2.BEG_AUM_AMT
)
如果您不知道PRODUCT可能在使用full outer join
时丢失哪个表格,并检查两个表格中的空值:
select coalesce(t1.product, t2.product) as product,
t1.POSTED_TRAN_AMT,
t2.BEG_AUM_AMT
from t1
full outer join t2 on t1.product = t2.product
where ( t1.product is null
or t2.product is null
or t1.POSTED_TRAN_AMT != t2.BEG_AUM_AMT
)
"我也尝试了不同的查询,但即使创建了连接,它也会将值相乘。你知道为什么会出现这种乘法吗?"
我的查询会比较汇总值,而您的版本则不会。一个变体是:
select A.product,
sum(A.POSTED_TRAN_AMT),
sum(B.BEG_AUM_AMT)
from A
join
B on A.product = B.product
and A.FISCAL_YEAR = B.FISCAL_YEAR
and A.ACCOUNTING_PERIOD = B.ACCOUNTING_PERIOD
where A.FISCAL_YEAR = 2017
and A.ACCOUNTING_PERIOD = 202
and A.ACCOUNT = '995050'
group by A.product having sum(A.POSTED_TRAN_AMT) != sum(B.BEG_AUM_AMT)
答案 1 :(得分:0)
这可能是一个过于复杂和缓慢的方法,但它没有假设每个PRODUCT
都在两个表中。
它对您已创建的两个摘要执行FULL OUTER JOIN
以比较它们。 FULL OUTER JOIN
将确保您的结果包含两个表中 中的每个产品。
WITH t1sum AS
(SELECT product,
SUM (posted_tran_amt) total_amount
FROM t1
WHERE fiscal_year = 2017
AND accounting_period = 202
AND account = '995050'
GROUP BY product),
t2sum AS
(SELECT product,
SUM (beg_aum_amt) total_amount
FROM t2
WHERE fiscal_year = 2017
AND accounting_period = 202
GROUP BY product)
SELECT NVL (t1sum.product, t2sum.product) product,
t1sum.total_amount,
t2sum.total_amount
FROM t1sum FULL OUTER JOIN t2sum ON t1sum.product = t2sum.product
WHERE (t1sum.total_amount IS NULL
AND t2sum.total_amount IS NOT NULL)
OR (t1sum.total_amount IS NOT NULL
AND t2sum.total_amount IS NULL)
OR (t1sum.total_amount != t2sum.total_amount)
答案 2 :(得分:0)
根据我对您的要求的理解,您的查询应该如下所示
(
select PRODUCT, sum(POSTED_TRAN_AMT) from t1
where FISCAL_YEAR = 2017 and ACCOUNTING_PERIOD = 202 and ACCOUNT = '995050'
group by product
minus
select PRODUCT, sum(_BEG_AUM_AMT) from t2
where FISCAL_YEAR = 2017 and ACCOUNTING_PERIOD = 202 group by product
)
union
(
select PRODUCT sum(_BEG_AUM_AMT) from t2
where FISCAL_YEAR = 2017 and ACCOUNTING_PERIOD = 202 group by product
minus
select PRODUCT, sum(POSTED_TRAN_AMT) from t1
where FISCAL_YEAR = 2017 and ACCOUNTING_PERIOD = 202 and ACCOUNT = '995050'
group by product
)