在执行求和时获取重复的行

xdnvmnnf  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(595)

我有三张table。contr\u id中前两个表之间以及第二个表和第三个表之间的连接是enc\u rcpt\u id。
合同

+----+----------+------+-----------+
| ID | CONTR_ID | CURR | TOT_CONTR |
+----+----------+------+-----------+
| 1  | 1        | USD  | 100       |
+----+----------+------+-----------+
| 2  | 2        | USD  | 80        |
+----+----------+------+-----------+

ins\ rcpt公司

+----+----------+-------------+-----------+----------+
| ID | CONTR_ID | ENC_RCPT_ID | RCPT_CURR | RCPT_AMT |
+----+----------+-------------+-----------+----------+
| 1  | 1        | 1           | USD       | 20       |
+----+----------+-------------+-----------+----------+
| 2  | 1        | 2           | USD       | 30       |
+----+----------+-------------+-----------+----------+
| 3  | 1        | 3           | USD       | 50       |
+----+----------+-------------+-----------+----------+
| 4  | 2        | 4           | USD       | 60       |
+----+----------+-------------+-----------+----------+
| 5  | 2        | 5           | USD       | 20       |
+----+----------+-------------+-----------+----------+

附件rcpt

+----+-------------+--------------+
| ID | ENC_RCPT_ID | ENC_RCPT_AMT |
+----+-------------+--------------+
| 1  | 1           | 5            |
+----+-------------+--------------+
| 2  | 1           | 15           |
+----+-------------+--------------+
| 3  | 2           | 5            |
+----+-------------+--------------+
| 4  | 2           | 10           |
+----+-------------+--------------+
| 5  | 2           | 15           |
+----+-------------+--------------+

我想显示如下输出:

+----------+------+-----------+----------+--------------+
| CONTR_ID | CURR | TOT_CONTR | RCPT_AMT | ENC_RCPT_AMT |
+----------+------+-----------+----------+--------------+
| 1        | USD  | 100       | 100      | 50           |
+----------+------+-----------+----------+--------------+
| 2        | USD  | 80        | 80       | 0            |
+----------+------+-----------+----------+--------------+

我写了下面的select查询,但它没有给出正确的结果。

SELECT C.contr_id, C.curr, C.tot_contr, IR.rcpt_amt, ER.enc_rcpt_amt 
FROM   @CONTR AS C 
       LEFT OUTER JOIN (SELECT contr_id, 
                               enc_rcpt_id, 
                               Sum(rcpt_amt) AS RCPT_AMT 
                        FROM   @INS_RCPT 
                        GROUP  BY contr_id, 
                                  enc_rcpt_id) AS IR 
                    ON IR.contr_id = C.contr_id 
       LEFT OUTER JOIN (SELECT ER1.enc_rcpt_id, 
                               Sum(ER1.enc_rcpt_amt) AS ENC_RCPT_AMT 
                        FROM   @ENC_RCPT AS ER1 
                               INNER JOIN @INS_RCPT AS IR1 
                                       ON IR1.enc_rcpt_id = ER1.enc_rcpt_id 
                        GROUP  BY ER1.enc_rcpt_id) AS ER 
                    ON ER.enc_rcpt_id = IR.enc_rcpt_id
u4dcyp6a

u4dcyp6a1#

你必须强迫所有的东西都在同一个谷粒上,所以你必须把它卷起来。

SELECT C.contr_id, C.curr, C.tot_contr, r.rcpt_amt, r.enc_rcpt_amt
from
contr c 
left outer join (select
contr_id,
sum(rcpt_amt) as rcpt_amt,
sum(enc_rcpt_amt) as enc_rcpt_amt
from

(SELECT contr_id, 
                               enc_rcpt_id, 
                               Sum(rcpt_amt) AS RCPT_AMT 
                        FROM   INS_RCPT 
                        GROUP  BY contr_id, 
                                  enc_rcpt_id) AS IR 
       LEFT OUTER JOIN (SELECT ER1.enc_rcpt_id, 
                               Sum(ER1.enc_rcpt_amt) AS ENC_RCPT_AMT 
                        FROM   ENC_RCPT AS ER1 
                               INNER JOIN INS_RCPT AS IR1 
                                       ON IR1.enc_rcpt_id = ER1.enc_rcpt_id 
                        GROUP  BY ER1.enc_rcpt_id) AS ER 
                    ON ER.enc_rcpt_id = IR.enc_rcpt_id 
  group by
  contr_id) r 
  on c.contr_id = r.contr_id
yshpjwxd

yshpjwxd2#

一种方法使用两个子查询:

select
    c.*,
    (
        select sum(i.rcpt_amt) rcpt_amt)
        from ins_rcpt i 
        where i.contr_id = c.contr_id
    ) rcpt_amt,
    (
        select coalesce(sum(r.enc_crpt_amt), 0)
        from enc_rpct r
        where exists (
            select 1 
            from ins_rcpt i
            where i.contr_id = c.contr_id and i.enc_rcpt_id = r.enc_rcpt_id 
        )           
    ) enc_crpt_amt
from contr c

相关问题