[英]Comparing two tables each column in oracle sql server using procedure
我需要一個程序來比較兩個具有相同模式但 diffe.net 數據的表
Table 1: dep_name,emp_name,sal
Table 2: dep_name,emp_name,sal,status
這是一個選項(第 1 - 10 行中的樣本數據)。
(雖然,我不確定你知道你使用的是哪個數據庫。它是 Oracle 還是 Microsoft SQL 服務器?我推測它是 Oracle。另外,屏幕截圖不支持你所描述的 - 表 2 中沒有status
列,但它存在於表 3 中——我認為(再次)——你想要的結果)。
SQL> with
2 t1 (dep_name, emp_name, sal) as
3 (select 'Public', 'xxxx', 20000 from dual union all
4 select 'Drug' , 'yyyy', 20000 from dual
5 ),
6 t2 (dep_name, emp_name, sal) as
7 (select 'Drug' , 'yyyy', 10000 from dual union all
8 select 'Public', 'xxxx', 20000 from dual union all
9 select 'Police', 'zzzz', 40000 from dual
10 )
11 select b.dep_name, b.emp_name, greatest(nvl(a.sal, 0), nvl(b.sal, 0)) sal,
12 case when a.sal = b.sal then 'Matched'
13 when a.sal <> b.sal then 'Unmatched'
14 else 'New'
15 end status
16 from t2 b left join t1 a on a.dep_name = b.dep_name
17 and a.emp_name = b.emp_name;
DEP_NA EMP_ SAL STATUS
------ ---- ---------- ---------
Public xxxx 20000 Matched
Drug yyyy 20000 Unmatched
Police zzzz 40000 New
SQL>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.