回答

收藏

如何联接两个表以获得以下结果?

技术问答 技术问答 117 人阅读 | 0 人回复 | 2023-09-12

我想加入两个表。
1 g9 P% i: j0 m- @1 ATABLE_A5 f, {" t" _" O
GROUP0     GROUP1     SUM_A
! a  N8 S  N8 w5 H. B5 C! v1 p---------------------------
# E" q( f* a1 a  L01           A         100         
# W* M' Q* p. C# p; e9 ~01           B         200( {& e$ D) q: H% C" [( z
04           D         700
+ I8 S7 d2 I0 ?0 I6 U+ k* r& uTABLE_B
% X  }6 l  v8 r% e: i( H: BGROUP0      GROUP1     SUM_B; Y) b+ s( D0 s# `: P! H
---------------------------
- u3 e2 Z3 I1 t01                     3006 F) ]+ R0 U6 X; c* @1 o. K* E
01           A         350+ `+ W4 T) ~+ u
02           B         400
7 ~6 \( u% z, i0 h) O. P2 S03           C         500
0 d+ T2 P3 y8 m7 M- o# g& [; X如何联接表以获得以下结果?6 B4 i1 J2 x. m9 W! V$ X5 j
GROUP0     GROUP1          SUM_A           SUM_B  a0 L7 H& T2 u  _; S# y
------------------------------------------------
8 s9 U" e4 Y6 m8 r8 m( T9 |01                             0            300
- ^. G8 L. t/ |7 {) V' P3 b01          A                100            350
/ _! |6 `5 K5 ~; F8 K% ~01          B                200              0
) O1 O/ L, e/ B0 Q6 x! S02          B                  0            400: f( A/ |1 X! A4 i0 m0 q7 Y
03          C                  0            500
! p+ o9 s8 ]% m* V6 E% _04          D                700              0
5 v" D7 |' a3 G1 l) R/ L: ~                6 E; f/ f& i4 `: {: U# H1 v# h
解决方案:; w9 Y9 R" N3 I( H
                ! ^& A% v8 }! N# {) U" F' Z) R' \/ O
6 g* K0 ^  Y/ T, J: L- s
- S0 J7 h1 x  K* [6 e5 a
                您希望所有内容都在第二个表中,然后匹配group0第一个表中的行或新行。
8 e. Z1 s& b7 C1 t4 g8 Y4 Y我认为这是join逻辑:4 ?5 x" ^* c/ X
select coalesce(t1.group0, t2.group0) as group0,
+ x7 i2 |# v% ]. q+ B$ t0 r$ d       coalesce(t1.group1, t2.group1) as group1,8 z# F7 P+ N9 i# {- \; p
       t1.sum_a, t2.sum_b
- u/ h1 u4 Y& O) x$ n# C" n  ffrom table1 t1 full outer join
) g% I5 J  h8 r+ ?/ a     table2 t2
, d6 G; s0 H- N. I: ]2 B0 v. |  F3 C     on t1.group0 = t2.group0
& D, F/ G" L- B2 ]+ {where (t2.group0 is not null and (t1.group1 = t2.group1 or t1.group0 is null)) or
2 K/ w7 @: o! g: |% C6 M      t2.group0 is null;4 ?# H) x% b6 @& [% R
使用union all以下命令更容易实现此逻辑:0 I4 M5 k3 Q* W! E! u2 h
select t2.group0, t2.group1, t1.sum_a, t2.sum_b
1 z- H- K: u0 A7 ?% ]! Pfrom table2 t2 left join# ^" o  `- n. N6 Z
     table1 t1
/ J8 ^/ ^0 S6 `0 O; g) B     on t2.group0 = t1.group0 and t2.group1 = t1.group1- ~( U0 u$ k+ h0 `8 w/ ?
union all) f, C/ `- ?6 \
select t1.group1, t1.group1, t1.suma, 0
% y; ~" E- k/ j% p/ X2 a$ r" R. Ffrom table1( l; g  e2 m+ Q4 o# f9 A
where not exists (select 1 from table2 t2 where t2.group0 = t1.group0);4 Y  Q% z/ m+ B" q
编辑:* e: Z+ b  B' r+ t7 R8 x5 F; r9 F
修改后的问题与原始问题完全不同。那很简单full outer join:
# ]; D/ B' h- W2 [; uselect coalesce(t1.group0, t2.group0) as group0,
8 o" F# K( D! e" N4 S- D/ }       coalesce(t1.group1, t2.group1) as group1,5 T, w: V( p* g7 Z2 Z) l0 }* j
       coalesce(t1.sum_a, 0) as sum_a, coalesce(t2.sum_b, 0) as sum_b( C2 V) o* m: k. [/ h- b  P% K& X
from table1 t1 full outer join" w) z6 V* G( H0 I" U
     table2 t2
. n. V  a3 W8 s  E. s& F' F9 R     on t1.group0 = t2.group0  and t1.group1 = t2.group1;
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则