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

332nm8kg  于 2021-06-24  发布在  Hive
关注(0)|答案(3)|浏览(246)

我尝试将三个表连接在一起,以检查正确的数据是否匹配。我有一张表,上面列出了支付佣金的所有账户以及佣金金额。我有表b和表c,这是两个有佣金计算的表。目标是将表a与表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\u ver和comm\u 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。

bqjvbblv

bqjvbblv1#

另一种选择可能是

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)
w80xi6nr

w80xi6nr2#

你很接近。只需要在case语句中再添加一个join和一个addition。这应该像一个if-elseif-else逻辑。所以它检查a.comm=b.comm,然后检查a.comm=c.comm。如果两者都不匹配,则if将设置为unmatched。这很好用,因为你说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;
flvlnr44

flvlnr443#

你可以做:

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

相关问题