我有兩個表,它們之間有一個共同的鍵,還有相當多的其他重要資訊;為了簡單起見,我將使用組合A和組合B。當一個組合被滿足時,哪個表的記錄數最多,就應該是我收集資訊的來源;在這種情況下,比如說ID。當數量相同時,優先考慮的是表1。
COMMONKEY列是我表中的組合/連接條件。
(Table 1)
SELECT '123 table1_id,'Comb A' commonkey from dual UNION
SELECT '124 table1_id,'Comb A' commonkey from dual UNION
SELECT '125 table1_id,'Comb A' commonkey from dual UNION
SELECT '126 table1_id,'Comb A' commonkey from dual UNION
SELECT '215 table1_id,'Comb B' commonkey from dual UNION
SELECT '216 table1_id,'Comb B' commonkey from dual UNION
SELECT '559' table1_id,'隨機組合1' commonkey from dual UNION
SELECT '560' table1_id,'隨機組合2' commonkey from dual ;
( Table 2 )
SELECT 'abc1' table2_id,'Comb A' commonkey from dual UNION )
SELECT 'abc2' table2_id,'Comb A' commonkey from dual UNION
SELECT 'abc3' table2_id,'Comb A' commonkey from dual UNION
SELECT 'abc4' table2_id,'Comb A' commonkey from dual UNION
SELECT 'xyz1' table2_id,'Comb B' commonkey from dual UNION
SELECT 'xyz2' table2_id,'Comb B' commonkey from dual UNION
SELECT 'xyz3' table2_id,'Comb B' commonkey from dual UNION
SELECT 'xyz2' table2_id,'Comb B' commonkey from dual UNION
SELECT '416abc1' table2_id,'隨機組合 91' commonkey from dual UNION
SELECT '416abc2' table2_id,'隨機組合92' commonkey from dual。
Result Set Expected :
ID COMMONKEY
123 Comb A
124 梳子A
125 梳子A
126 梳子A
xyz1 梳子B
xyz2 梳子B
xyz3 梳子B
559 隨機組合 1
560隨機組合 1
416abc1隨機組合 91
416abc2隨機組合 92
(圖片顯示的是excel中線索資料的截圖;需求和策略是用顏色映射的,以使其快速理解)
我需要使用SQL生成結果集,如下所示:
當table1.commonkey = table2.commonkey擊中時,我需要-- 如果table1有10個ID,table2有5個ID -> 從table1中挑選10個ID。
- 如果表1有15個ID,表2有30個ID -> 從表2中挑選30個ID。
- 如果表1有4個ID,表2有4個ID -> 從表1中挑選4個ID。 (當相等時,選擇表1的ID)
- 當公共鍵沒有發生匹配時,防止交叉連接,并將行集線性地添加到結果表中。
編輯:我最初走的路線是
a left join b where b。 key IS null ;
a full outer join b where b。 key is NULL or a.key is NULL 。
用A-B或B-A結果集來實作變通,但這兩種方法都是非常錯誤的。收集Delta集或Exclusion集并不順利。
uj5u.com熱心網友回復:
這里有一個選擇,請看代碼中的注釋
SQL>/span> with
2 --樣本資料
3 a (id, ckey) as
4 (select '123'/span>, 'ca' from dual union all
5 select '124'/span>, 'ca' from double union all
6 select '125'/span>, 'ca' from double union all
7 select '126'/span>, 'ca' from double union all
8 select '215'/span>, 'cb' from double union all
9 select '216'/span>, 'cb' from double union all
10 select '551'/span>, 'r1' from double union all
11 select '552', 'r2' from dual
12 )。)
13 b (id, ckey) as
14 (select 'abc1', 'ca' from dual union all
15 select 'abc2'。'ca' from double union all
16 select 'abc3', 'ca' from double union all
17 select 'abc4', 'ca' from double union all
18 select 'xyz1', 'cb' from dual union all
19 select 'xyz2', 'cb' from dual union all
20 select 'xyz3'。'cb' from dual union all
21 select '9991'/span>, 'r3' from dual union all
22 select '9992', 'R4' from double
23 )。)
24 --計算每個CKEY(公共鍵)的記錄
25 tempa as
26 (select id, ckey, count(*) over (partition by ckey) cnt
27 from a
28 )。)
29 tempb as
30 (select id, ckey, count(*) over (partition byckey) cnt
31 from b
32 )
33 --最終查詢
34 select distinct
35 case when a.cnt >= b.cnt then a.id
36 else b.id
37 end id,
38 a.ckey
39 from tempa a join tempb b on b.ckey = a.ckey
40 union all
41 select ckey, id from a
42 where not exists (select null from b where a. ckey = b.ckey)
43 union all
44 select ckey, id from b
45 where not exists (select null from a where a. ckey = b.ckey)
46 order by 1, 2;
其結果是
ID CKEY
---- -----
r1 551[/span
r2 552[/span
r3 9991[/span
r4 9992 9992
xyz1 cb
xyz2 cb
xyz3 cb
123 ca
124 ca
125 ca
126 ca
11 rows 選擇。
SQL>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/320155.html
標籤:
