如何连接两个没有唯一 ID 的表
How to join two Tables which have no unique ID
我正在尝试向左加入 Table 1 到 table 2
Table1 Table2
ID Data ID Data2
1 r 1 q
2 t 1 a
3 z 2 x
1 u 3 c
我离开后加入了这两个 Tables 我想要这样的东西
Table1+2
ID Data Data2
1 r a
2 t x
3 z c
1 u q
而不是
Table1+2
ID Data Data2
1 r q
2 t x
3 z c
1 u q
我的问题是:是否有可能告诉 table 2,如果您为 table 1 使用了某些东西,请不要使用它并给我下一个值。我是否必须将其设置为 T-SQL 或新列,如果此 ID 存在,我可以在其中列出,如果不是 1(数字数据),则写入 2。我怎么解决这个问题?
提前告诉你。
使用ROW_NUMBER
DECLARE @Table1 TABLE (ID INT, DATA VARCHAR(10))
DECLARE @Table2 TABLE (ID INT, DATA VARCHAR(10))
INSERT INTO @Table1
VALUES
(1, 'r'),
(2, 't'),
(3, 'z'),
(1, 'u')
INSERT INTO @Table2
VALUES
(1, 'q'),
(1, 'a'),
(2, 'x'),
(3, 'c')
SELECT
A.*,
B.DATA
FROM
(SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY(SELECT NULL)) RowId FROM @Table1) A INNER JOIN
(SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY(SELECT NULL)) RowId FROM @Table2) B ON A.ID = B.ID AND
A.RowId = B.RowId
由于两个表上的 ID 都没有唯一性,因此让我们为其添加一些唯一性。
所以它可以用来加入。
window 函数 ROW_NUMBER 可用于此。
给出预期结果的示例解决方案:
DECLARE @TestTable1 TABLE (ID INT, Data VARCHAR(1));
DECLARE @TestTable2 TABLE (ID INT, Data VARCHAR(1));
INSERT INTO @TestTable1 VALUES (1,'r'),(2,'t'),(3,'z'),(1,'u');
INSERT INTO @TestTable2 VALUES (1,'q'),(1,'a'),(2,'x'),(3,'c');
select
t1.ID, t1.Data,
t2.Data as Data2
from (
select ID, Data,
row_number() over (partition by ID order by Data) as rn
from @TestTable1
) t1
left join (
select ID, Data,
row_number() over (partition by ID order by Data) as rn
from @TestTable2
) t2 on t1.ID = t2.ID and t1.rn = t2.rn;
注意: 由于 LEFT JOIN,这确实假定 table2 中相同 ID 的数量等于或小于 table1 中的相同 ID 的数量。但如果不是这种情况,您可以将其更改为 FULL JOIN。
Returns :
ID Data Data2
1 r a
1 u q
2 t x
3 z c
要获得另一个结果,可以通过不同的方式实现。
这其实是比较常见的情况。
想要全部来自 Table 1,但对于 Table 1.
的每条记录只能从 Table 2 获得一个值
1) top 1 with ties
与 order by rownumber()
组合
select top 1 with ties
t1.ID, t1.Data,
t2.Data as Data2
from @TestTable1 t1
left join @TestTable2 t2 on t1.ID = t2.ID
order by row_number() over (partition by t1.ID, t1.Data order by t2.Data desc);
top 1 with ties
只会显示那些 row_number()
= 1
2) 在子查询中使用 row_number:
select ID, Data, Data2
from (
select
t1.ID, t1.Data,
t2.Data as Data2,
row_number() over (partition by t1.ID, t1.Data order by t2.Data desc) as rn
from @TestTable1 t1
left join @TestTable2 t2 on t1.ID = t2.ID
) q
where rn = 1;
3) 只是一个简单的分组依据和一个最大值:
select t1.ID, t1.Data, max(t2.Data) as Data2
from @TestTable1 t1
left join @TestTable2 t2 on t1.ID = t2.ID
group by t1.ID, t1.Data
order by 1,2;
所有 3 个都给出相同的结果:
ID Data Data2
1 r q
1 u q
2 t x
3 z c
我正在尝试向左加入 Table 1 到 table 2
Table1 Table2
ID Data ID Data2
1 r 1 q
2 t 1 a
3 z 2 x
1 u 3 c
我离开后加入了这两个 Tables 我想要这样的东西
Table1+2
ID Data Data2
1 r a
2 t x
3 z c
1 u q
而不是
Table1+2
ID Data Data2
1 r q
2 t x
3 z c
1 u q
我的问题是:是否有可能告诉 table 2,如果您为 table 1 使用了某些东西,请不要使用它并给我下一个值。我是否必须将其设置为 T-SQL 或新列,如果此 ID 存在,我可以在其中列出,如果不是 1(数字数据),则写入 2。我怎么解决这个问题? 提前告诉你。
使用ROW_NUMBER
DECLARE @Table1 TABLE (ID INT, DATA VARCHAR(10))
DECLARE @Table2 TABLE (ID INT, DATA VARCHAR(10))
INSERT INTO @Table1
VALUES
(1, 'r'),
(2, 't'),
(3, 'z'),
(1, 'u')
INSERT INTO @Table2
VALUES
(1, 'q'),
(1, 'a'),
(2, 'x'),
(3, 'c')
SELECT
A.*,
B.DATA
FROM
(SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY(SELECT NULL)) RowId FROM @Table1) A INNER JOIN
(SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY(SELECT NULL)) RowId FROM @Table2) B ON A.ID = B.ID AND
A.RowId = B.RowId
由于两个表上的 ID 都没有唯一性,因此让我们为其添加一些唯一性。
所以它可以用来加入。
window 函数 ROW_NUMBER 可用于此。
给出预期结果的示例解决方案:
DECLARE @TestTable1 TABLE (ID INT, Data VARCHAR(1));
DECLARE @TestTable2 TABLE (ID INT, Data VARCHAR(1));
INSERT INTO @TestTable1 VALUES (1,'r'),(2,'t'),(3,'z'),(1,'u');
INSERT INTO @TestTable2 VALUES (1,'q'),(1,'a'),(2,'x'),(3,'c');
select
t1.ID, t1.Data,
t2.Data as Data2
from (
select ID, Data,
row_number() over (partition by ID order by Data) as rn
from @TestTable1
) t1
left join (
select ID, Data,
row_number() over (partition by ID order by Data) as rn
from @TestTable2
) t2 on t1.ID = t2.ID and t1.rn = t2.rn;
注意: 由于 LEFT JOIN,这确实假定 table2 中相同 ID 的数量等于或小于 table1 中的相同 ID 的数量。但如果不是这种情况,您可以将其更改为 FULL JOIN。
Returns :
ID Data Data2
1 r a
1 u q
2 t x
3 z c
要获得另一个结果,可以通过不同的方式实现。
这其实是比较常见的情况。
想要全部来自 Table 1,但对于 Table 1.
1) top 1 with ties
与 order by rownumber()
select top 1 with ties
t1.ID, t1.Data,
t2.Data as Data2
from @TestTable1 t1
left join @TestTable2 t2 on t1.ID = t2.ID
order by row_number() over (partition by t1.ID, t1.Data order by t2.Data desc);
top 1 with ties
只会显示那些 row_number()
= 1
2) 在子查询中使用 row_number:
select ID, Data, Data2
from (
select
t1.ID, t1.Data,
t2.Data as Data2,
row_number() over (partition by t1.ID, t1.Data order by t2.Data desc) as rn
from @TestTable1 t1
left join @TestTable2 t2 on t1.ID = t2.ID
) q
where rn = 1;
3) 只是一个简单的分组依据和一个最大值:
select t1.ID, t1.Data, max(t2.Data) as Data2
from @TestTable1 t1
left join @TestTable2 t2 on t1.ID = t2.ID
group by t1.ID, t1.Data
order by 1,2;
所有 3 个都给出相同的结果:
ID Data Data2
1 r q
1 u q
2 t x
3 z c