2015-11-02 61 views
0

表1入世對非唯一列

1 A 1 1 
2 A 1 2 
5 A 1 1 
6 B 2 1 

表2

1 1 12 
2 2 45 
3 5 22 
4 6 21 

table1.col1是一個FK到table2.col2

你想去的地方COL2 = A重複值,並且具有col2 = AA:

1 A 1 1 
2 A 1 2 
5 A 1 1 
6 B 2 1 
7 AA 1 1 <- New 
8 AA 1 2 <- New 
9 AA 1 1 <- New 

你如何將表2連接到新的結果集,使得AA中存在的值也存在於AA中?

結果想:

1 A 1 1 | 1 1 12 
2 A 1 2 | 2 2 45 
5 A 1 1 | 3 5 22 
6 B 2 1 | 4 6 21 
7 AA 1 1 | 1 1 12 
8 AA 1 2 | 2 2 45 
9 AA 1 1 | 3 5 22 
+0

7,8,9可以被添加到表1或僅在運行時產生的AA值?基於現有數據?你可以添加一列到table1的parentID和使用遞歸? – xQbert

+0

AA的值是派生的運行時,對於兩個表的列1都是自動增量標識列。不,我不能添加一列到表1,但我可以做一個子查詢和解析 – LearningJrDev

回答

1

未經測試:

的邏輯似乎是固體,但我沒有數據,測試環境試試這個... 你可以使用一個工會和(內嵌視圖或公共表格表達式)來完成此操作。

首先我們用兩組所需數據(下面的內聯視圖A)構建table1。這種方法使聯接變得簡單。這是通過使用聯合聲明和硬編碼AA值來實現的,同時將該集合限制爲只有A,然後在基本集合中進行聯合。

然後我們再回到table2照常。

我使用了row_number()以及col 2的排序來標識單個值以通過增加最大ID。 1表示a的第二行的2的第一行,而a的第3行的3表示基於6的種子,這是表1中的最大值。

我使用parent_ID來始終標識要加入到table2的相關記錄。

聯視圖

Select * --(though you should spell out desired columns) 
from (Select ROW_NUMBER() OVER(ORDER BY Col2)+C.mID, 'AA', col3, col4, col1 as Parent_ID 
     from table1 
     CROSS JOIN (select max(col1) mID from table1) C 
     where table1.col2 = 'A' 
    record 
     UNION ALL 
     Select col1, Col2, col3, col4, col1 as Parent_ID 
     from table1) A 

INNER JOIN table2 
on table2.col2 = A.parent_ID 

CTE:

With cte as (Select ROW_NUMBER() OVER(ORDER BY Col2)+C.mID col1, 'AA' col2, col3, col4, col1 as Parent_Id 
    FROM table1 
    CROSS JOIN (select max(col1) mID from table1) C 
    WHERE table1.col2 = 'A' 
    UNION ALL 
    SELECT col1, Col2, col3, col4, col1 as Parent_Id 
    from table1) 

SELECT * --(though you should spell out desired columns) 
FROM cte 
INNER JOIN table2 
    on table2.col2 = cte.Parent_Id 
0

孤立地考慮每個值A/B/AA和使用窗函數來找到COL3和COL4滯後超前

將每個「prev_col3,col3,next_col3,prev_col4,col4,next_col4」視爲唯一的「上下文」標識符並加入。這是我們可以避免混淆第7行和第9行的數據;他們對col3和col4有不同的prev/next lag/lead值。

我們需要控制空的情況下(我使null爲-1)的工作聯接。

您可以複製/粘貼到SQL服務器以瞭解其工作:

CREATE TABLE #TABLE1 (col1 INT, col2 varchar(5), col3 INT, col4 INT) 
CREATE TABLE #TABLE2 (col1 INT, col2 INT, col3 INT) 

INSERT INTO #TABLE1 
select 1 col1,'A' col2, 1 col3, 1 col4 union 
select 2 col1,'A' col2, 1 col3, 2 col4 union 
select 5 col1,'A' col2, 1 col3, 1 col4 union 
select 6 col1,'B' col2, 2 col3, 1 col4 union 
select 7 col1,'AA' col2, 1 col3, 1 col4 union 
select 8 col1,'AA' col2, 1 col3, 2 col4 union 
select 9 col1,'AA' col2, 1 col3, 1 col4 

INSERT INTO #TABLE2 
select 1 col1, 1 col2, 12 col3 union 
select 2 col1,2 col2, 45 col3 union 
select 3 col1,5 col2, 22 col3 union 
select 4 col1,6 col2, 21 col3 


select 
    Bu.col1, bu.col2, bu.col3, bu.col4, t2.col1, t2.col2, t2.col3 
from 
    (
    select 
     col1, col2, 
     lag(col3) over (order by col1 asc) prev_col3, 
     col3, 
     lead(col3) over (order by col1 asc) next_col3, 
     lag(col4) over (order by col1 asc) prev_col4, 
     col4, 
     lead(col4) over (order by col1 asc) next_col4 
    from 
     #TABLE1 t1 where col2 in ('A') 
    ) A 
    join 
    (/*bu big union*/ 
    select 
     col1, col2, 
     lag(col3) over (order by col1 asc) prev_col3, 
     col3, 
     lead(col3) over (order by col1 asc) next_col3, 
     lag(col4) over (order by col1 asc) prev_col4, 
     col4, 
     lead(col4) over (order by col1 asc) next_col4 
    from 
     #TABLE1 t1 where col2 in ('A') 
    UNION 
    select 
     col1, col2, 
     lag(col3) over (order by col1 asc) prev_col3, 
     col3, 
     lead(col3) over (order by col1 asc) next_col3, 
     lag(col4) over (order by col1 asc) prev_col4, 
     col4, 
     lead(col4) over (order by col1 asc) next_col4 
    from 
     #TABLE1 t1 where col2 in ('AA') 
    ) bu 
    on 
    ( 
    a.col3 = bu.col3 and isnull(a.prev_col3,-1) = isnull(bu.prev_col3,-1) and 
    isnull(a.next_col3,-1) = isnull(bu.next_col3,-1) and 
    a.col4 = bu.col4 and isnull(a.prev_col4,-1) = isnull(bu.prev_col4,-1) and 
    isnull(a.next_col4,-1) = isnull(bu.next_col4 ,-1) 
    ) 
    join 
    #TABLE2 t2 
    on 
    a.col1 = t2.col2 
    UNION 
    select 
     t1.col1, t1.col2, t1.col3, t1.col4, 
     t2.col1, t2.col2, t2.col3 
    from 
     #TABLE1 t1 
     join #TABLE2 t2 on t1.col1 = t2.col2 
    where t1.col2 = 'B' 
    order by 1 asc 

drop table #TABLE1 
drop table #TABLE2 
0
declare @maxCol1 int 

    select @maxCol1 = max(col1) 
    from table1 


    select a.col1, 
    a.col2, 
    a.col3, 
    a.col4, 
    b.col1, 
    b.col2, 
    b.col3 
    from table1 a 
    join table2 b 
    on a.col1 = b.col2 

    union all 

    select a.col1 + @maxCol1 as col1, 
    a.col2 + a.col2 as col2, 
    a.col3, 
    a.col4, 
    b.col1, 
    b.col2, 
    b.col3 
    from table1 a 
    join table2 b 
    on a.col1 = b.col2 
    where a.col2 = 'A'