0

表格1:

Id          Customer No_
7044900804  Z0172132
7044900804  Z0194585
7044907735  Z0172222
7044907735  Z0172222
7044910337  Z0172216
7044911903  Z0117392

我只想从 table1中获取具有相同 Id 和不同 Customer No_的值。

Id          Customer No_
7044900804  Z0172132
7044900804  Z0194585

我已经尝试使用查询来查找重复项,但它不会从 table1 中过滤具有相同 ID 和相同客户编号的值。

SELECT  Id
      , [Customer No_]
  FROM table1
GROUP BY Id, [Customer No_]
HAVING COUNT(*) > 1

你能帮我解决T-SQL查询吗?感谢所有的建议。

4

5 回答 5

3

尝试这个

SELECT * FROM table1 WHERE Id IN
(
    SELECT  Id
      FROM table1
    GROUP BY Id
    HAVING COUNT(DISTINCT [Customer No_]) > 1
)

SQL 小提琴演示

于 2013-08-28T11:28:20.687 回答
1

我认为最简单的方法是将表连接在一起两次,其中连接了 ID,但 Customer No_s 不匹配。

select t1.id, t1.[customer No_], t2.[customer No_]
from table1 t1 
inner join table1 t2
on t1.id = t2.id
and t1.[customer No_] != t2.[customer No_]
于 2013-08-28T11:25:27.313 回答
0

请试试:

SELECT * FROM (
    SELECT 
        *, 
        COUNT(*) OVER (PARTITION BY id, [Customer No_]) C1, 
        COUNT(*) OVER (PARTITION BY id) C2 
    FROM 
        YourTable
)x WHERE C1<>C2
于 2013-08-28T11:24:51.523 回答
0

尝试:

SELECT ID, [Customer No_]
FROM table1
WHERE ID IN (SELECT ID
             FROM table1
             GROUP BY ID
             HAVING count(*) > 1)

SQL小提琴

于 2013-08-28T11:25:22.230 回答
0

询问:

SELECT t1.Id
      ,t1.[Customer No_]
FROM table1 t1
 JOIN (SELECT Id, COUNT(*) as cnt
       FROM table1 
       GROUP BY id)
 ON t1.id = t2.id
WHERE t2.cnt > 1
于 2013-08-28T11:26:54.537 回答