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

目前有这样的一个表,统计用户每天的访问的网址的信息,但是每个用户访问的网站肯定是有重复的数据的,我想在这个统计的数据中,查询出,用户统计的不同网址的总数,并且安装访问网址的高低进行排列,求该sql语句

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 )

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-20
select count(distinct 网址) from表 group by 用户
第2个回答  推荐于2018-02-27
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本回答被提问者和网友采纳
相似回答