如何解决根据金额总和加入
基本上有2个文件
文件 1 示例
参考 | 金额 |
---|---|
AA1 | 1000 |
文件 2 示例
参考 | Match_No | 侧面 | 金额 |
---|---|---|---|
AA1 | 123 | 账本 | 1000 |
BB1 | 123 | 声明 | 500 |
CC1 | 123 | 声明 | 500 |
现在要求使用来自文件 1 的引用,从文件 2 中提取 Match_No,其中 side = 'Ledger'
我认为这个脚本应该是:-
选择 file2.match_no
从 file1 加入 file2 on file1.reference = file2.reference
其中边 = '分类帐'
现在在提取 Match_No where side = Ledger 之后,对于相同的 Match_No,从文件 2 中提取所有引用 where side ='Statement' 和 file2.amounts 的总和(where side='Statement')= file2.amount(where side = 分类帐)
解决方法
我就是这么理解你说的。看看是否有帮助。
SQL> with
2 -- Sample data; you have it already & don't type it
3 file_1 (reference,amount) as
4 (select 'aa1',1000 from dual),5 file_2 (reference,match_no,side,amount) as
6 (select 'aa1',123,'Ledger',1000 from dual union all
7 select 'bb1','Statement',500 from dual union all
8 select 'cc1',500 from dual
9 ),10 -- Useful code begins here.
11 -- Query you posted (I added "B.AMOUNT" and used it in line #21)
12 qyp as
13 (select b.match_no,b.amount
14 from file_1 a join file_2 b on a.reference = b.reference
15 where b.side = 'Ledger'
16 )
17 -- The final query
18 select b.reference
19 from file_2 b join qyp q on q.match_no = b.match_no
20 where b.side = 'Statement'
21 and q.amount = (select sum(c.amount)
22 from file_2 c
23 where c.match_no = b.match_no
24 and c.side = 'Statement'
25 );
REF
---
cc1
bb1
SQL>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。