求SQL语句

lovebeyondforever 2006-06-13 10:59:59
要求

表diary
1.选出top 20 人
2.根据最近30天发布日记的数量排行 datediff(d,diary_timed,getdate())<30
3.要求显示每个人的日记数量
4.显示姓名 userid
5.显示标题 title

现在等 ..马上揭帖!

select xx from diary where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0 其他不会写了...
谢谢
...全文
294 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
k_arches 2006-06-14
  • 打赏
  • 举报
回复
要求

表diary
1.选出top 20 人
2.根据最近30天发布日记的数量排行 datediff(d,diary_timed,getdate())<30
3.要求显示每个人的日记数量
4.显示姓名 userid
5.显示标题 title

重复记录的关键是 要显示:title
itblog 2006-06-13
  • 打赏
  • 举报
回复
select a.userid,a.title,b.日记数量
from diary a inner
(
select top 20 userid,日记数量=count(title) from diary where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0 group by userid
)
b
on a.userid=b.userid
  • 打赏
  • 举报
回复
一句sql不难,但是没有效率,这个应该是一天或几次的吧?

可以参考我写的blog

http://blog.csdn.net/liuxiaoyi666/archive/2006/05/10/722837.aspx
lovebeyondforever 2006-06-13
  • 打赏
  • 举报
回复
userid 就是姓名
忘了加一个diary_id 就是日记id

userid title 数量
我不信 一二三四五 1

itblog 2006-06-13
  • 打赏
  • 举报
回复
不对啊,你这里也没有涉及到姓名字段的,你能贴出几条完整的记录吗?
lovebeyondforever 2006-06-13
  • 打赏
  • 举报
回复
是姓名重复
aini美丽
我不信
钢琴家
helenawhy
rrrnnn
网上情缘
gdgzapplelwp
gdgzapplelwp
gdgzapplelwp
xshm
amy788
怀旧
怀旧
华哥
雪落
嫣一
aini美丽
无烟
怀旧
爱我
itblog 2006-06-13
  • 打赏
  • 举报
回复
这样呢?

select top 20 userid,title,count(distinct title) as 日记数量
from diary
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title
lovebeyondforever 2006-06-13
  • 打赏
  • 举报
回复
还是有重复.

闹心啊..

主要是没有测试环境 谢谢大家
itblog 2006-06-13
  • 打赏
  • 举报
回复
不知道你说的重复是怎么个重复~
itblog 2006-06-13
  • 打赏
  • 举报
回复
select top 20 userid,title,count(title) as 日记数量
from
(
select * from diary a where not exists(select 1 from diary where userid=a.userid and title=a.title and updatetime>a.updatetime)
)
b
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title
lovebeyondforever 2006-06-13
  • 打赏
  • 举报
回复
to LouisXIV(夜游神) ( ) 信誉:100

执行效率太低 执行时间太久了. 不过还是要感谢你
lovebeyondforever 2006-06-13
  • 打赏
  • 举报
回复
谢谢
to itblog(BegCSharp) ( ) 信誉:100 你的还是有重复的姓名
to LouisXIV(夜游神 也有错误我正在改

如果调试好
开100分 感谢2位
LouisXIV 2006-06-13
  • 打赏
  • 举报
回复
-_-括号贴错

select distinct userid,title,数量
from diary a
inner join
(
select top 20
userid,
(select count(title) from diary where datediff(d,diary_timed,getdate())<30 group by userid) as 数量
from diary c
order by 数量 desc
)b
on a.userid=b.userid
LouisXIV 2006-06-13
  • 打赏
  • 举报
回复
--改一下,没有数据测试就是不爽

select distinct userid,title,数量
from diary a
inner join
(
select top 20
userid,
(select count(title) from diary where datediff(d,diary_timed,getdate())<30) group by userid) as 数量
from diary
order by 数量
desc
)b
on a.userid=b.userid
LouisXIV 2006-06-13
  • 打赏
  • 举报
回复
--try

select distinct userid,title,数量
from diary a
inner join
(
select top 20
userid,
(select count(userid) from diary where datediff(d,diary_timed,getdate())<30) group by userid) as 数量
from diary
order by 数量
desc
)b
on a.userid=b.userid
itblog 2006-06-13
  • 打赏
  • 举报
回复
select distinct top 20 userid,title,count(title) as 日记数量
from diary
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title



or
select top 20 * from
(
select distinct userid,title,count(title) as 日记数量
from diary
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title
) b

lovebeyondforever 2006-06-13
  • 打赏
  • 举报
回复
速度果然快 谢谢

但是有重复的人...

20人里面不能有重复的 谢谢
itblog 2006-06-13
  • 打赏
  • 举报
回复
select top 20 userid,title,count(title) as 日记数量
from diary
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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