写SQL(每个用户访问最多的URL)

jerryweiaaa 2018-01-06 12:21:29
一张表,2列,UID(用户编号),URL(用户访问的页面),求每个用户访问次数最多的URL(用户访问一次表中会有一条记录)
...全文
383 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
山城忙碌人 2018-01-07
  • 打赏
  • 举报
回复
sqlserver 2005 以上写法




drop table tbuser;
create table tbuser(
uid bigint ,
url varchar(100),
udate varchar(20)
);
insert into tbuser
select 1,'www.csdn.net',getdate() union all
select 1,'www.csdn.net',getdate() union all
select 1,'www.163.com',getdate() union all
select 1,'www.baidu.com',getdate() union all
select 1,'www.csdn.net',getdate() union all
select 1,'www.163.com',getdate() union all
select 1,'www.csdn.net',getdate() union all

select 2,'www.baidu.com',getdate() union all
select 2,'www.baidu.net',getdate() union all
select 2,'www.baidu.com',getdate() union all
select 2,'www.baidu.com',getdate() union all
select 2,'www.csdn.net',getdate() union all

select 3,'www.163.com',getdate() union all
select 3,'www.163.net',getdate() union all
select 3,'www.163.com',getdate() union all
select 3,'www.163.com',getdate() union all
select 3,'www.163.com',getdate() union all
select 3,'www.csdn.com',getdate()



select * from (
select row_number() over(partition by uid
order by murlnum desc) as num,* from (
select count(url) as murlnum,uid,url from tbuser group by uid,url
) as source ) a
where a.num=1




下次建议楼主把测试表,测试数据sql写上,不要让别人帮你写

Rotel-刘志东 2018-01-06
  • 打赏
  • 举报
回复
select uid, count(url) from tb group by uid
jerryweiaaa 2018-01-06
  • 打赏
  • 举报
回复
要求返回值是URL(访问最多的那个),而不是URL的次数。

56,675

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧