select tidb_version();
Release Version: v6.1.0
Edition: Community
Git Commit Hash: 1a89decdb192cbdce6a7b0020d71128bc964d30f
Git Branch: heads/refs/tags/v6.1.0
UTC Build Time: 2022-06-05 05:15:11
GoVersion: go1.18.2
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
select * from Tab_A
1 1 1 A01 01 111 111.00
2 2 2 A01 01 112 111.00
3 3 3 A02 02 113 111.00
4 4 4 A02 02 112 111.00
5 5 5 A01 01 111 111.00
6 6 6 A02 02 113 111.00
7 5 7 A01 01 111 88.00
8 6 8 A02 02 113 88.00
select * from Tab_B
1 B01
2 B01
3 B02
4 B02
5 B02
6 B02
select * from Tab_C
1 C01 3.00
2 C01 22.00
3 C01 32.00
4 C01 5.00
5 C01 6.00
6 C01 9.00
select Tab_A.name AAA,Tab_A.id,Tab_B.name BBB,Tab_A.amt Aamt, Tab_C.amt Bamt,IFNULL(Tab_C.amt, 0)
FROM Tab_A left join Tab_B on Tab_A.bid=Tab_B.id
left join Tab_C on Tab_A.cid=Tab_C.id and Tab_A.type=‘01’
where Tab_A.num=112
A01 2 B01 111.00 22.00 22.00
A02 4 B02 111.00 NULL 0
select Tab_A.name AAA,Tab_B.name BBB,COUNT(Tab_A.id) times,
SUM(Tab_A.amt) - SUM(IFNULL(Tab_C.amt, 0)) amt
FROM Tab_A left join Tab_B on Tab_A.bid=Tab_B.id
left join Tab_C on Tab_A.cid=Tab_C.id and Tab_A.type=‘01’
where Tab_A.num=112
group by Tab_A.name,Tab_B.name
A01 B01 1 89.00
A02 NULL 1 106.00