sql统计行数,但是需要去重中间的重复数据

如题所述

第1个回答  2022-11-16

userId: user id

url: url visited by the user

SELECT userId, COUNT(DISTINCT url)

FROM tab

GROUP BY userId

ORDER BY COUNT(DISTINCT url) DESC

扩展资料:

group by 解决重复数据的个数统计适用于各种关系型数据库,如oracle,SQL Server

查询重复的数据

select * from (select v.xh,count(v.xh) num from sms.vehicle v group by v.xh) where num>1;

select v.xh,count(v.xh) num from sms.vehicle v group by v.xh having count(v.xh)=2;

删除重复的数据

create table mayong as (select distinct* from sms.vehicle);

delete from sms.vehicle ;

insert into sms.vehicle select * from mayong;

在oracle中,有个隐藏了自动rowid,里面给每条记录一个唯一的rowid,如果想保留最新的一条记录,就可以利用这个字段,保留重复数据中rowid最大的一条记录就可以了。

下面是查询重复数据的一个例子:

select a.rowid,a.* from 表名 a 

where a.rowid != (select max(b.rowid) from 表名 b where a.字段1 = b.字段1 and a.字段2 = b.字段2 )

相似回答
大家正在搜