Sql 从联接创建唯一值表
我有两张不同地址的桌子。一个是我们已经存档的位置表,另一个是新业务。我的想法是,我将这两个表格的坐标连接起来,以显示新业务和当前业务之间是否存在冲突 我发现,在新业务中,我们有一个位置与我们已有的三个位置相匹配 当我进行简单的内部连接时,当我真正想要显示4条记录时,我会返回3条记录(1条来自新记录,3条来自当前记录)。我尝试过其他的连接和联合以及子查询,但没有成功。我知道有办法,但就是想不出来Sql 从联接创建唯一值表,sql,sql-server,Sql,Sql Server,我有两张不同地址的桌子。一个是我们已经存档的位置表,另一个是新业务。我的想法是,我将这两个表格的坐标连接起来,以显示新业务和当前业务之间是否存在冲突 我发现,在新业务中,我们有一个位置与我们已有的三个位置相匹配 当我进行简单的内部连接时,当我真正想要显示4条记录时,我会返回3条记录(1条来自新记录,3条来自当前记录)。我尝试过其他的连接和联合以及子查询,但没有成功。我知道有办法,但就是想不出来 SELECT * FROM NewBusiness INNER JOIN Live L ON N.La
SELECT *
FROM NewBusiness
INNER JOIN Live L ON N.Latitude = L.Latitude AND N.Longitude = L.Longitude
提前感谢所以我可以通过工会获得我认为您需要的东西。这可能不是最好的方法,但看起来它很有效。我做了一把SQL小提琴来展示它 你可以在这里看到小提琴: 为了测试,我创建了两个表,Live和NewBusiness。 他们就是这样创造的
CREATE TABLE Live
([ID] varchar(1), [latitude] int, [longitude] int)
;
INSERT INTO Live
([ID], [latitude], [longitude])
VALUES
('a', 1, 2),
('b', 1, 2),
('c', 1, 2),
('d', 4, 3),
('e', 5, 4),
('k', 5, 7),
('l', 5, 9),
('M', 5, 7)
;
及
我使用的查询是
(
SELECT L.ID
,L.Latitude
,L.Longitude
FROM Live L
INNER JOIN NEWBUSINESS N
ON L.Latitude = N.Latitude AND L.Longitude = N.Longitude
)
UNION
(
SELECT N.ID
,N.LATITUDE
,N.LONGITUDE
FROM NEWBUSINESS N
INNER JOIN Live L
ON N.Latitude = L.Latitude AND N.Longitude = L.Longitude
GROUP BY N.ID
,N.LATITUDE
,N.LONGITUDE
)
联盟的第一部分将获得所有与新业务相匹配的实时内容。联盟的第二部分获得了新业务中所有在现场进行匹配的东西。然后将结果合并在一起
表:
Live
ID latitude longitude
a 1 2
b 1 2
c 1 2
d 4 3
e 5 4
k 5 7
l 5 9
M 5 7
**NewBusiness**
ID latitude longitude
f 1 2
g 5 2
h 1 8
i 6 3
z 5 7
y 12 4
x 5 7
**Query Results**
ID Latitude Longitude
a 1 2
b 1 2
c 1 2
f 1 2
k 5 7
M 5 7
x 5 7
z 5 7
这样做是否可以满足您的需要(对于任何一个表中与另一个表中的记录相匹配的记录,它将计数一次): 也许不是最好的答案(它捕获同一个表中的重复行),但仍能正常工作
select distinct 'oldbiz', l.id, l.latitude, l.longitude
from Live l, NewBusiness n
where l.latitude = n.latitude
and l.longitude = n.longitude
union all
select distinct 'newbiz', n.id, n.latitude, n.longitude
from Live l, NewBusiness n
where l.latitude = n.latitude
and l.longitude = n.longitude
您能添加样本数据和预期结果吗?对于我来说,不清楚您想要实现什么以及数据在表中的结构。请添加数据和预期结果以更好地理解问题。很高兴但不确定我如何发布样本数据。我可以说两个表中的列结构完全相同。这就是为什么我想得到一个具有唯一记录的表。因此,目前我的Live table 1中有三条记录与新业务表中的记录相匹配。当我进行内部连接时,我得到了三条记录,但从左到右的列重复,我的新业务位置重复了三次。我真正想看到的是四条具有唯一列集的记录
WITH cte1
AS
(
SELECT
N.latitude
, N.longitude
, ROW_NUMBER() OVER (ORDER BY N.latitude, N.longitude) r1
FROM NewBusiness N
)
,
cte2
AS
(
SELECT
N.latitude
, N.longitude
, ROW_NUMBER() OVER (PARTITION BY N.r1 ORDER BY N.latitude, N.longitude) r2
FROM
cte1 N
JOIN Live L ON
N.Latitude = L.Latitude
AND N.Longitude = L.Longitude
)
SELECT
latitude
, longitude
, 'NewBusiness' sourceTable
FROM cte2
WHERE r2 = 1
UNION ALL
SELECT
latitude
, longitude
, sourceTable
FROM
(
SELECT DISTINCT
latitude
, longitude
, r2
, 'Live' sourceTable
FROM cte2
) Q
select distinct 'oldbiz', l.id, l.latitude, l.longitude
from Live l, NewBusiness n
where l.latitude = n.latitude
and l.longitude = n.longitude
union all
select distinct 'newbiz', n.id, n.latitude, n.longitude
from Live l, NewBusiness n
where l.latitude = n.latitude
and l.longitude = n.longitude