数据库面试题

shexinwei 2011-04-24 04:58:04
今天去腾讯实习生面试,面试官给了一个题目。

现在有30张表,每张表记录了当天的登录用户信息(一个用户由于时间不同可以有多项),每张表大约有8亿项。

现在要求使用SQL操作查找出这个月登陆次数最多的前N个用户。

大家多多提供思路啊。

后面还会继续加分。
...全文
650 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aleaf 2011-07-17
  • 打赏
  • 举报
回复
应该是union all
select username,sum(cnt) as cnt from
(
select username,count(*) as cnt from tbl_name1 group by username
union all
select username,count(*) as cnt from tbl_name2 group by username
......
union all
select username,count(*) as cnt from tbl_name30 group by username
)
group by username
order by cnt desc
或者干脆就
select username,count(*) as cnt from
(
select username from tbl_name1
union all
select username from tbl_name2
......
union all
select username from tbl_name30
)
group by username
order by cnt desc
aleaf 2011-07-17
  • 打赏
  • 举报
回复
select username,sum(cnt) as cnt from
(
select username,count(*) as cnt from tbl_name1
union
select username,count(*) as cnt from tbl_name2
......
union
select username,count(*) as cnt from tbl_name30
)
order by cnt desc

这是全部的倒序,想取前N个的话自己再加个限制语句
DanDing2015 2011-07-12
  • 打赏
  • 举报
回复
学习学习
csdn网速很慢 2011-07-09
  • 打赏
  • 举报
回复
登陆次数前N的用户至少在一个表排前N
每张表前TOP N找出 N*30条排序
tanguolovepig 2011-07-08
  • 打赏
  • 举报
回复
select * from tablename where DATEDIFF(day,RecordTime,getdate())<=30 order by(numline)

8,028

社区成员

发帖
与我相关
我的任务
社区描述
高性能数据库开发
社区管理员
  • 高性能数据库开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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