所以这里有一些样本信息:
ROW | PHONE | ID | NAME
_________________________________________
1 000-000-0000 3 Company1
2 000-000-0000 38 Company2
1 123-456-7890 0 Company3
2 123-456-7890 0 Company4
3 123-456-7890 0 Company5
1 197-865-4329 0 Company6
2 197-865-4329 18 Company7
1 654-987-1230 74 Company8
2 654-987-1230 2 Company9
1 951-753-8462 2 Company10
2 951-753-8462 3 Company11
3 951-753-8462 3 Company12
4 951-753-8462 3 Company13
使用以下代码从表中提取此信息:
USE MyTable
GO
SELECT ROW_NUMBER() OVER (PARTITION BY AI.telephone1 ORDER BY AI.telephone1) AS Row
,AI.telephone1 AS [Main Phone #]
,AI.new_id AS [ID]
,AI.name AS [Account Name]
,AI.emailaddress1 AS [Email Address]
,AI.contactname AS [Primary Contact]
FROM AccountsInfo AI
WHERE AI.telephone1 IN (
SELECT telephone1
FROM MyDatabase.dbo.AccountsInfo AI
WHERE telephone1 != 'NULL'
AND telephone1 != '--'
GROUP BY telephone1
HAVING COUNT(*) > 1
)
ORDER BY telephone1
我现在需要做的是,但无法绕过我的脑袋,遍历每个分区并仅提取具有匹配ID号的结果。请参阅下文,了解我希望桌子最终看起来像什么......
PHONE | ID | NAME
_________________________________________
123-456-7890 0 Company3
123-456-7890 0 Company4
123-456-7890 0 Company5
951-753-8462 3 Company11
951-753-8462 3 Company12
951-753-8462 3 Company13
正如您所看到的,公司3,4和5进入了结果,因为他们三个都有匹配的ID号和匹配的电话号码。公司11,12和13制作了它们的分区,因为它们具有匹配的ID号。公司10没有被拉,因为它的ID号不同于11,12和13。
我需要做的是让代码迭代每个分区(按电话号码分区)并检查分区中每行的ID号与分区中的其他行。最终结果将只是在其分区内的其他位置重复ID号的行。
如果您需要任何其他信息,请与我们联系。
答案 0 :(得分:2)
这个怎么样?
SELECT AI.telephone1 AS [Main Phone #]
,AI.new_id AS [ID]
,AI.name AS [Account Name]
,AI.emailaddress1 AS [Email Address]
,AI.contactname AS [Primary Contact]
FROM AccountsInfo AI
INNER JOIN
(
SELECT telephone1,new_id
FROM MyDatabase.dbo.AccountsInfo AI
WHERE telephone1 != 'NULL'
AND telephone1 != '--'
GROUP BY telephone1, new_id
HAVING COUNT(*) > 1
) T
ON AI.telephone1 = T.telephone1
AND AI.new_id = T.new_id
ORDER BY AI.telephone1
答案 1 :(得分:0)
您也可以使用having
:
select
"phone", "id"
from
"data"
group by "phone", "id"
having ( min ( "row" ) < max( "row" ));
有关更多信息和可能性,请查看Joe Celko's Thinking in Sets
请参阅SQL-Fiddle