有一张表,需要查询表中重复数据(名称重复,代码不同的数据),现在是这样写的select 名称 ,代码 from tab
group by 名称, 代码  having COUNT(*)>1  这样写有一个问题,查询结果中包含,名称相同并且代码相同的语句,想得到结果是,名称相同,但价码不相同的结果。知道的朋友帮解答下,不胜感激!

解决方案 »

  1.   

    SELECT DISTINCT 代码 FROM tab
      

  2.   

    楼上正解
    select distinct 代码 from tb where 名称 in
    (select 名称 from tb 
    group by 名称
    having count(*)>1)
      

  3.   

    distinct 只是查询不重复的数据吧?  如果 有2个或2个以上重复的只查询出1个,但是不重复的也会查询出来的。
      

  4.   

    select a.* 
    from tab a inner join 
    (
    select *
    from (
    select row_number() over(partition by name,code ORDER by name DESC) AS number,* 
    from tab) as t 
    where  number>1) b on a.name=b.name and a.code=b.code拿去试试吧,name = 名称 ,code = 代码
      

  5.   

    上面是得到名称相同,代码相同的,
    select *
    from (
    select row_number() over(partition by name,code ORDER by name DESC) AS number,* 
    from tab) as t 
    where  number=1
    这是得到名称相同,代码不相同的(ps:tab = 你的table)
      

  6.   


    想查处重复数据的名称,代码 如下
    select distinct name,code 
     from (
     select row_number() over(partition by name,code ORDER by name DESC) AS number,* 
     from tab) as t 
     where  number>1
    总之灵活运用,万事OK
      

  7.   

    纠正一个东西:
    distinct 是消除重复的值.
    你试下看看这个是不是你要的效果:
    select 名称,价码 from tab group by 价码,名称 having COUNT(价码)=1 
      

  8.   

    select distinct 名称,代码 from tab.这就可以了
      

  9.   

     谢谢各位,解决了  select distinct A.名称,A.代码
      FROM tab as A join
      tab as B
      on A.名称=B.名称
      where A.代码<>B.代码