文章詳情頁
案例討論:Oracle數(shù)據(jù)庫的分組問題
瀏覽:111日期:2023-11-21 08:53:07
曾經(jīng),有個(gè)用戶提出了以下的一個(gè)問題,隨即有很多朋友提供了他們的解決方案,我們的專家將這些記錄整理出來,供大家參考學(xué)習(xí)。 問:要求把表B按照CLM001分組求和計(jì)算score的值,然后加到表A中對(duì)應(yīng)CLM001的score的值。如何寫更新表A的SQL語句?表A:CLM001;;;;;SCORE001;;;;;;0002 0003;0004 0其中CLM001是主鍵。表B:ID CLM001 SCORE0;;;001;;;;121;;;001;;;;242;;;002;;;1103;;;004;;;2204;;;004;;;;20;;;答: with r as {select CLM001 ,sum(score) as total from b group by CLM001}update a set score = (select total from r where r.CLM001 =a.CLM001)where exists (select 1 from r where r.CLM001 =a.CLM001)點(diǎn)評(píng):此方法需要寫存儲(chǔ)過程,否則在程序中沒法用。create or replace procedure p_testasv_clm001 a.clm001%type;cursor c_record isselect distinct clm001 from b;beginopen c_record;loopfetch c_record into v_clm001;exit when c_record%notfound;update a set score =(select sum(score) from b where clm001=v_clm001)where clm001=v_clm001;end loop;end;點(diǎn)評(píng):這是在PL/SQL中的語句,此問題用PL/SQL解決也不失為一個(gè)簡(jiǎn)單的方法。update a set a.score = a.score + (select b.score from a a, (select CLM001,sum(score) from b group by CLM001) b where a.CLM001 = b.CLM001)點(diǎn)評(píng):最佳答案,此類問題用視圖解決很方便!
標(biāo)簽:
Oracle
數(shù)據(jù)庫
排行榜
