我想要完成的任务:
Dataset 1
Name1
Name2
Name3
Dataset 2
Number1
Number2
Number3
将成为2列:
dataset1 dataset2
Name1 Number1
Name2 Number2
Name3 Number3
我的数据集1& 2将始终具有相等的行。 只要两个名字没有链接到同一个号码,哪个号码链接到我不关心的号码,反之亦然。
如何使用SQL / SQL Server解决此问题?
答案 0 :(得分:2)
如果您不想向表中添加标识列,可以使用ROW_NUMBER()函数,如下所示:
SELECT
T1.Col1,
T2.Col1
FROM
(SELECT Col1, ROW_NUMBER() OVER (ORDER BY Col1) AS N FROM Table1) T1
INNER JOIN
(SELECT Col1, ROW_NUMBER() OVER (ORDER BY Col1) AS N FROM Table2) T2
ON T1.N = T2.N
此处,将Table1和Table2替换为表的名称,并将Col1替换为要从这两个表中输出的列(或列)的名称。
答案 1 :(得分:1)
向两个表添加标识列,并根据这些列执行连接
ALTER TABLE Table1
ADD ID INT IDENTITY(1,1) NOT NULL
ALTER TABLE Table2
ADD ID INT IDENTITY(1,1) NOT NULL
SELECT Table1.dataset1col , Table2.dataset2Col
From Table1 INNER JOIN Table2
ON Table1.ID = Table2.ID
答案 2 :(得分:1)
这可能对您有用:
;WITH cte1 (name, rn)
AS (SELECT Name,
row_number()
OVER(
ORDER BY Name) rn
FROM Dataset1),
cte2 (Number, rn)
AS (SELECT Number,
row_number()
OVER(
ORDER BY Number) rn
FROM Dataset2)
SELECT name,
Number
FROM cte1
JOIN cte2
ON cte1.rn = cte2.rn
答案 3 :(得分:1)
WITH Table1 AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Dataset1) as Rnk,Dataset1
FROM TA1
)
With Table2 AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Dataset2) as Rnk, Dataset2
FROM TA2
)
Select Table.Dataset1 as 'DataSet1', Table2.DataSet2 as 'DataSet2'
From Table1
inner join Table2 on Table1.Rnk = Table2.Rnk
因为你没有添加表名所以我认为它是TA1和TA2。
答案 4 :(得分:0)
编写查询的另一种方法是:
select row_number() over (order by Names asc) as rownum,
Names
into #Temp1
from NameTable
select row_number() over (order by Numbers asc) as rownum,
Numbers
into #Temp2
from NumberTable
select Names, Numbers
from #Temp1
inner join #Temp2 on #Temp1.rownum = #Temp2.rownum
答案 5 :(得分:0)
有三种可能的解决方案。
首先:使用以下技巧(警告:小数据集时使用此功能)
SELECT DISTINCT tbl1.col1, tbl2.col2
FROM
(SELECT FirstName AS col1, ROW_NUMBER() OVER (ORDER BY FirstName) Number FROM dbo.User) tbl1
INNER JOIN
(SELECT LastName AS col2, ROW_NUMBER() OVER (ORDER BY LastName) Number FROM dbo.User) tbl2
ON tbl1.Number = tbl2.Number
第二:使用变量表临时存储结果。此解决方案适用于相对较大的数据集。 (大约记录到100s)
<强>第三强> 如mmhasannn所述,在两个表中使用identitfy字段。但我最不喜欢这种方法,因为我们需要修改我们的数据库结构。
推荐:使用变量表方法