基于一个键连接三个表,将数据放入同一列

时间:2019-09-18 18:45:42

标签: sql hive

我试图将三个表连接在一起,以检查数据是否正确匹配。我有表A,该表列出了支付佣金的所有帐户以及该佣金的金额。我有表B和表C,这两个表中都有佣金计算。目的是将表A与表和表C进行比较,并从两个表中撤回金额以确保匹配。我苦苦挣扎的部分是,表A包含了所有作为基本人口的帐户。表B有一些,表C有一些。一个帐户将在表B或表C中,但都不在两个表中。我想从表A中提取付款,然后验证到表B或C中的付款(无论发生哪种情况),并与佣金相同。然后,我正在做一个比较两个字段并告诉我是否匹配的案例。

+---------+---------+-----+------+
| Table A |         |     |      |
+---------+---------+-----+------+
| Account | Uniq_ID | Pay | Comm |
| 12345   | ABCD    | 100 | 10   |
| 23456   | OLPOL   | 25  | 2    |
| 45678   | LKJHG   | 200 | 15   |
| 96385   | LKJ67   | 250 | 26   |
+---------+---------+-----+------+
+---------+---------+-----+------+
| Table B |         |     |      |
+---------+---------+-----+------+
| Account | Uniq_ID | Pay | Comm |
| 12345   | ABCD    | 100 | 8    |
| 45678   | LKJHG   | 200 | 15   |
+---------+---------+-----+------+
+---------+---------+-----+------+
| Table C |         |     |      |
+---------+---------+-----+------+
| Account | Uniq_ID | Pay | Comm |
| 23456   | OLPOL   | 25  | 2    |
| 96385   | LKJ67   | 250 | 32   |
+---------+---------+-----+------+

我正在尝试将我的结果显示在名为pay_ver和comm_verf的列中,并将其与之匹配的表B或C中的数据填充。我希望必须输出看起来像这样。...


+---------+---------+-----+----------+------+-----------+---------+
| Output  |         |     |          |      |           |         |
+---------+---------+-----+----------+------+-----------+---------+
| Account | Uniq_ID | Pay | Pay_verf | comm | comm_Verf | Matched |
| 12345   | ABCD    | 100 | 100      | 10   | 8         | No      |
| 23456   | OLPOL   | 25  | 25       | 2    | 2         | Yes     |
| 45678   | LKJHG   | 200 | 200      | 15   | 15        | Yes     |
| 96385   | LKJ67   | 250 | 250      | 26   | 32        | No      |
+---------+---------+-----+----------+------+-----------+---------+

这是我用于将表A到B以及表A到C连接起来的代码,但是我已经在两个单独的查询中完成了此操作,并提供了两个输出。我希望能够做到这一点,所以我只有一个输出。

select a.account, a.uniq_id, a.pay, b.pay as pay_verf, a.comm, b.comm as comm_verf,
CASE WHEN a.comm = b.comm THEN 'MATCHED'
    ELSE 'UNMATCHED'
    END as Matched
    from tblA a
    left join tblB b
    on a.account = b.account
    and a.uniq_id = b.uniq_id;

我不能只想出如何也可以不添加额外的列而将其加入表C。

3 个答案:

答案 0 :(得分:1)

您非常亲密。只需在您的case语句中添加一个额外的join和一个WHEN即可。这应该像其他逻辑一样。因此,它将检查a.comm = b.comm,然后检查a.comm = c.comm。如果两者都不匹配,则将设置为不匹配。效果很好,因为您声明ID不能同时位于B和C中。

select a.account, a.uniq_id, a.pay, b.pay as pay_verf, a.comm, b.comm as comm_verf,
CASE WHEN a.comm = b.comm THEN 'MATCHED'
     WHEN a.comm = c.comm THEN 'MATCHED'
    ELSE 'UNMATCHED'
    END as Matched
    from tblA a
    left join tblB b
    on a.account = b.account
    and a.uniq_id = b.uniq_id;
    left join tblB c
    on a.account = c.account
    and a.uniq_id = c.uniq_id;

答案 1 :(得分:1)

您可以这样做:

select
  account, uniq_id, pay,
  pay_total as pay_verf,
  comm,
  comm - comm_total as comm_verf,
  case when comm = comm_total then 'Yes' else 'No' end as matched
from (
  select
    a.account, a.uniq_id, a.pay, a.comm,
    coalesce(b.pay, 0) + coalesce(c.pay, 0) as pay_total,
    coalesce(b.comm, 0) + coalesce(c.comm, 0) as comm_total
  from table_a a
  left join table_b b on a.account = b.account
  left join table_c c on a.account = c.account
) x

答案 2 :(得分:0)

另一个选择可能是类似的

SELECT a.account, a.uniq_id, a.pay, bc.pay as pay_verf, a.comm, bc.comm as comm_verf
FROM a left join (
    SELECT * from b
    UNION ALL
    SELECT * from c
) bc on (a.account = bc.account and a.uniq_id = bc.uniq_id)