一定要記住 :如果多表連線後存在金額的 加減操作的話,那麼在表連線的時候一定要按連線字段進行分組 加總金額後 再做表連線
即便是left join 也不例外
否則就會出現下面的資料。 可以看到[other_table_credit]列是rs2表中的乙個條資料,結果和rs1 關聯後,該金額被多增加了5倍,顯然這是不正確的,
所以需要把rs1也彙總之後再做表連線
cust_no
normal_bal
other_table_credit
bail_receipt_national_debt
sum_bal
510290000003327
76000000
1284800000
01360800000
510290000003327
140000000
1284800000
01424800000
510290000003327
150000000
1284800000
01434800000
510290000003327
229000000
1284800000
01513800000
510290000003327
117200000
1284800000
01402000000
510290000003327
90000000
1284800000
01374800000
select '2013-05-31',
tab1.cust_no,
tab2.cust_name,
tab1.normal_bal,
tab1.other_table_credit,
tab1.bail_receipt_national_debt,
tab1.num_bal
from (select nvl(rs1.cust_num, nvl(rs2.cust_num, rs3.cust_num)) as cust_no,
rs1.normal_bal as normal_bal,
rs1.concerned_bal as concerned_bal,
rs1.subprime_bal as subprime_bal,
rs1.doubtful_bal as doubtful_bal,
rs1.loss_bal as loss_bal,
nvl(rs2.other_table_credit, 0) as other_table_credit,
nvl(rs3.bail_receipt_national_debt, 0) as bail_receipt_national_debt,
(nvl(rs1.normal_bal, 0) + nvl(rs1.concerned_bal, 0) +
nvl(rs1.subprime_bal, 0) + nvl(rs1.doubtful_bal, 0) +
nvl(rs1.loss_bal, 0) + nvl(rs2.other_table_credit, 0) -
nvl(rs3.bail_receipt_national_debt, 0)) as num_bal
from (---------rs1:拆放同業餘額明細數
---------rs1:再次算出每個客戶下面五級分類的餘額各有多少
select t.cust_num,
decode(t.cbrc_grade,'n',fnc_cmb_ccy_conversion(t.begin_date,t.currency,nvl(t.balance,0),'cny'),0) as normal_bal ,
decode(t.cbrc_grade,'c',fnc_cmb_ccy_conversion(t.begin_date,t.currency,nvl(t.balance,0),'cny'),0) as concerned_bal,
decode(t.cbrc_grade,'s',fnc_cmb_ccy_conversion(t.begin_date,t.currency,nvl(t.balance,0),'cny'),0) as subprime_bal ,
decode(t.cbrc_grade,'d',fnc_cmb_ccy_conversion(t.begin_date,t.currency,nvl(t.balance,0),'cny'),0) as doubtful_bal ,
decode(t.cbrc_grade,'l',fnc_cmb_ccy_conversion(t.begin_date,t.currency,nvl(t.balance,0),'cny'),0) as loss_bal
from cmb_dm_markeyinfotab t
where t.on_off_flag = '0'
and t.trade_type in ('2', '10')
and t.begin_date = '2013-05-31'
and t.cust_num = '510290000003327'
) rs1
full join
---------rs2:其他表內授信明細數
(select t2.cust_num, sum(nvl(t2.bal, 0)) as other_table_credit
from (select t.cust_num,
fnc_cmb_ccy_conversion(t.begin_date,t.currency,t.balance,'cny') as bal
from cmb_dm_markeyinfotab t
where t.on_off_flag = '0'
and t.trade_type = '1'
and t.begin_date = '2013-05-31'
union all
select t1.cust_num,
fnc_cmb_ccy_conversion(t1.begin_date,t1.currency,t1.balance,'cny') as bal
from cmb_dm_assetsaletab t1
where t1.busi_type = '1'
and t1.fina_code is not null --20130620 add.
and t1.begin_date = '2013-05-31') t2 where t2.cust_num = '510290000003327'
group by t2.cust_num) rs2
on rs1.cust_num = rs2.cust_num
---------rs3:保證金、銀行存單、國債明細數
left join (select t3.cust_no as cust_num,
sum(nvl(t3.coll_mk_val, 0)) as bail_receipt_national_debt
from cmb_dm_collainfotab t3
where t3.coll_typ in ('30', '41')
and t3.begin_date <= '2013-05-31'
and t3.end_date > '2013-05-31'
group by t3.cust_no) rs3
on rs1.cust_num = rs3.cust_num) tab1
-------------tab2:將tab1的明細關聯上對公客戶資訊表取出客戶名稱、集團號、集團名稱
left join (select t4.cust_num,
t4.cust_name,
nvl(t4.cust_grp_no,t4.cust_num) as cust_grp_no,
nvl(t4.cust_grp_nam,t4.cust_name) as cust_grp_nam
from cmb_dm_ccustomertab t4
where t4.begin_date <= '2013-05-31'
and t4.end_date > '2013-05-31') tab2
on tab1.cust_no = tab2.cust_num;
mysql 的多表連線
1 select from a b 預設是笛卡爾積 2 內連線 兩者一樣 顯示內連線 select from a inner join b on 條件 隱式內連線 select from a,b where 條件 3 外連線 3.1 左外連線 select from a left outer joi...
MySQL中的多表連線
現有表r,s如下 笛卡爾積 select from r,s 結果 注 不需要任何條件。結果為兩張表函式相乘 3x3 9 連線型別 分為三種 交叉連線 內連線 外連線。交叉連線 cross join 沒有where子句的交叉連線將產生連線表的笛卡爾積。select from r cross join ...
常用的多表連線查詢
內連線 inner join 返回的是兩個表中匹配連線條件的行的集合 1.等值內連線 2.非等值內連線 3.自然連線 natural join 自然連線 自動找相同字段相等的值作為連線條件,若沒有相同字段,則相當於做笛卡爾積運算,即dept表中的每一行都有test表中的每一行去匹配,返回 行數 行數...