比如有如下的数据格式:
user_id product_id
12 67
12 90
345 4
12 90
12       90
23 90
  发现第2行、第4行、第5行重复了,都是  12  90。  于是要去掉一个,只保留一个,即最后的输出应该为:
user_id   product_id
12        67
12        90
345       4
23        90
该怎么写呢?
我的写法:select distinct user_id,product_id from table group by product_id但是好像不太对。

解决方案 »

  1.   

    group by product_id去掉就行。
      

  2.   


    select distinct user_id,product_id from table order by product_id
      

  3.   

    create table tb (user_id int, product_id int)
    insert tb select
    12,67 union all
    select 12,90 union all
    select 345,4 union all
    select 12,90 union all
    select 12,90 union all
    select 23,90select * from tb
    /*
    user_id product_id
    12 67
    12 90
    345 4
    12 90
    12 90
    23 90
    */
    select distinct USER_ID,product_id from tb 
    /*
    USER_ID product_id
    12 67
    12 90
    23 90
    345 4
    */drop table tb
      

  4.   

    create table tb (user_id int, product_id int)
    insert tb select
    12,67 union all
    select 12,90 union all
    select 345,4 union all
    select 12,90 union all
    select 12,90 union all
    select 23,90select * from tb
    /*
    user_id product_id
    12 67
    12 90
    345 4
    12 90
    12 90
    23 90
    */
    select distinct USER_ID,product_id from tb 
    /*
    USER_ID product_id
    12 67
    12 90
    23 90
    345 4
    */
    select USER_ID,product_id
    from (select ROW_NUMBER() over(partition by user_id,product_id  order by user_id,product_id  )rn,* from tb)a
    where rn=1
    /*
    USER_ID product_id
    12 67
    12 90
    23 90
    345 4
    */
    go
    drop table tb
      

  5.   

    group by product_id是分组统计,这里不用,如果你还要统计相同记录个数,则加上group by可以。
      

  6.   


    select * from tb
    union 
    select * from tb
      

  7.   

    把group by 后面去掉就行了
      

  8.   

    十分感谢上面各位的回答。就是说正确的应该是:select distinct user_id,product_id from table因为我一直以为distinct只能去重紧跟其后的那个字段(即本例中的user_id)。逗号以后的字段(product_id)是没有去重效果的。看来不是这样啊。
      

  9.   

    select distinct user_id,product_id from table
    orselect user_id,product_id from table group by user_id,product_id
      

  10.   

    select distinct user_id,product_id from table不要分组
      

  11.   

    为啥子 不先google一下 呢?
      

  12.   


    --有两种简便的方法
    --1、用Distinct 这个函数是去除重复行
    select distinct user_id,product_id from table--2、用Group By 这个函数是分组查询
    select user_id,product_id from table group by user_id,product_id希望对你有用
      

  13.   


    --有两种简便的方法
    --1、用Distinct 这个函数是去除重复行
    select distinct user_id,product_id from table--2、用Group By 这个函数是分组查询
    select user_id,product_id from table group by user_id,product_id希望对你有用
      

  14.   

    这里去掉Group By  product_id直接查就行 select distinct user_id,product_id from table 
      

  15.   

    select distinct user_id,product_id from table
      

  16.   


    select distinct user_id,product_id from table你自己已经基本上完成了。只是后面不该加的group by不对