比如有如下的数据格式:
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但是好像不太对。
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但是好像不太对。
select distinct user_id,product_id from table order by product_id
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
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
select * from tb
union
select * from tb
orselect user_id,product_id from table group by user_id,product_id
--有两种简便的方法
--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希望对你有用
--有两种简便的方法
--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希望对你有用
select distinct user_id,product_id from table你自己已经基本上完成了。只是后面不该加的group by不对