表1 geowellconnectionwellgroup 資料類似下面:
id1 id2
1500003107 1500003117
1500003109 1500003113
1500003109 1500003115
1500003111 1500003110
1500003112 1500003117
1500003104 1500003103
1500003105 1500003308
1500003109 1500003100
1500003109 1500003110
1500003111 1500003103
1500003112 1500003108
1500003112 1500003116
1500003204 1500003208
表2 registerwells 資料類似下面
id name
1500011111 bz34-2-3d-s-s2
1500011112 bz34-2-3d-s-s3
1500011113 bz34-2-3d-u-dj
1500011114 bz34-2-4d
1500011115 bz34-2-4d-l-s3
1500011116 bz34-2-4d-l-s4
1500011117 bz34-2-4d-s-s3
1500011118 bz34-2-4d-s-s4
1500011119 bz34-2-4d-u-dj
1500011120 bz34-2-p1
1500011121 bz34-2-p1-e-cm
1500011122 bz34-2-p1-l-s2
1500011123 bz34-2-p1-l-s3
1500011124 bz34-2-p1-s-s2
1500002219 cb-b-20
目的想得到下面的結果集,在表1中的id對應出name:
id1 id2 wellname wellname_1
1500003104 1500003103 jz9-3-e1-6 jz9-3-e1-5
1500003105 1500003306 jz9-3-e1-7 jz9-3-a07
1500003105 1500003106 jz9-3-e1-7 jz9-3-e1-8
1500003105 1500003308 jz9-3-e1-7 jz9-3-a09s
1500003107 1500003108 jz9-3-e2-2 jz9-3-e2-3
1500003107 1500003100 jz9-3-e2-2 jz9-3-e1-2
1500003107 1500003113 jz9-3-e2-2 jz9-3-e3-4
1500003107 1500003117 jz9-3-e2-2 jz9-3-e2-7
1500003109 1500003101 jz9-3-e2-4 jz9-3-e1-3
1500003109 1500003108 jz9-3-e2-4 jz9-3-e2-3
1500003109 1500003102 jz9-3-e2-4 jz9-3-e1-4
1500003109 1500003119 jz9-3-e2-4 jz9-3-e1-9
1500003109 1500003117 jz9-3-e2-4 jz9-3-e2-7
方案1,取笛卡兒積
select a,d,w1,w2 from
select * from (
with g as (
select distinct wellid as id1, connectionwellid as id2
from ogdp.geowellconnectionwellgroup
select a,d,w1,w2 from (
select g.id1 as a ,g.id2 as b ,b.wellname as w1 from g
join ogdp.registerwells b on g.id1 = b.wellid ) aa
cross join
select g.id1 as c,g.id2 as d,b.wellname as w2 from g
join ogdp.registerwells b on g.id2 = b.wellid ) bb
) f
,geowellconnectionwellgroup s
where s.wellid =f.a and s.connectionwellid = f.d
方案2 :直接做join
with g as (
select distinct wellid as id1, connectionwellid as id2
from ogdp.geowellconnectionwellgroup a )
select first.id1,first.id2,first.wellname,r2.wellname from (
select g.id1,g.id2,r1.wellid,r1.wellname from g
join ogdp.registerwells r1 on g.id1 = r1.wellid ) first
join ogdp.registerwells r2 on first.id2 = r2.wellid
方案2效率高些
sqlserver 比較兩個表的列
一 問題 給了兩個各有四五十個列的表,找出他們相同的列和不同的列 二 查詢兩個表的列,存在臨時表 a b都是臨時表,當前連線斷開後自動刪除 rank over order hswpooby syscolumns.name desc as 是sql2005支援的,在每行記錄前加上自增序號 idenhs...
excel中兩列匹配,取匹配列的後一列資料
剛在處理excel資料時,遇到如下問題 已知a列的英文名和b列的中文名,現有一批c列的英文名,想根據a b列的對應規則,輸出c列對應的中文名。但是c列的順序和a列順序不一致,也不一定所有內容都被包含在a列。舉例 a b c d prvnce name 省名稱 accs nbr latn name 本...
求最大子列和問題兩種演算法比較
給定k個整數組成的序列,連續子列 被定義為,其中 1 i j k。最大子列和 則被定義為所有連續子列元素的和中最大者。例如給定序列,其連續子列有最大的和20。現要求你編寫程式,計算給定整數序列的最大子列和。很容易想到一種最暴力的方式,那就是逐個逐個的累計,從i 0到i n,總共需要n次大迴圈,並且對...