编写一个SQL查询,检索列中具有相同值的所有不同行,其中这些行的计数大于1

编写一个SQL查询,检索列中具有相同值的所有不同行,其中这些行的计数大于1,sql,count,distinct,Sql,Count,Distinct,基本上,我需要找到超过一个名称的所有ID 所以输出应该是 (2,a)(2,b)(4,a)(4,b)(4,c) 身份证名称 1A 2A 2 b 3 c 3 c 4A 4 b 4 c 我有个问题要问 select * from (select distinct * from test) t group by t.id having count(*) > 1 但我认为可能有一个更简单的解决方案,所以是的,有一个简单的解决方案吗,如果有,是什么?提前感谢。以下是一些您可以尝试的东西: Se

基本上,我需要找到超过一个名称的所有ID 所以输出应该是

 (2,a)(2,b)(4,a)(4,b)(4,c) 
身份证名称 1A 2A 2 b 3 c 3 c 4A 4 b 4 c

我有个问题要问
select * from (select distinct * from test) t group by t.id having count(*) > 1

但我认为可能有一个更简单的解决方案,所以是的,有一个简单的解决方案吗,如果有,是什么?提前感谢。

以下是一些您可以尝试的东西:

Select
  Id,
  Name
FROM test 
Group By Id, Name
Having Count(Distinct(Name)) > 1
select id, name
from T
where exists (
  select * from T as Tcopy
  where Tcopy.id = T.id
  and Tcopy.name <> T.name
);

with IDmultipleNames(id) as (
  select id
  from T
  group by id
  having min(name) <> max(name)
)
  select id, name
  from T
  where T.id in (
    select id from IDmultipleNames
  )

以下是您可以尝试的几件事情:

select id, name
from T
where exists (
  select * from T as Tcopy
  where Tcopy.id = T.id
  and Tcopy.name <> T.name
);

with IDmultipleNames(id) as (
  select id
  from T
  group by id
  having min(name) <> max(name)
)
  select id, name
  from T
  where T.id in (
    select id from IDmultipleNames
  )

Thanx对于输入,我在Sybase中尝试过,删除了Groupby works中的名称,Thanx.Thanx对于输入,我在Sybase中尝试过,删除了Groupby works中的名称,Thanx。