能否建议我如何编写查询来检索以下内容?
我需要找到 A 中的值重复但 B 中的值不同的任何行
A B
4567 2
852125 9
444 8
2547 25
255 4
256 1
2547 25
2547 27
259 4
2547 25
应该返回
2547 27
2547 25
因为 2547 在 B 列中有两个不相同的值
感谢期待
P
最佳答案
with cte1 as (
select distinct A, B from Table1
), cte2 as (
select A, B, count(*) over(partition by A) as cnt from cte1
)
select
A, B
from cte2
where cnt > 1
或
with cte as (
select distinct
A, B, min(B) over(partition by A) as m1, max(B) over(partition by A) as m2
from Table1
)
select
A, B
from cte
where m1 <> m2
关于sql-server - SQL Server 2008 根据 1 个唯一列查找行并在第二列中查找重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27256765/