[再次提问]文章统计:怎么样最近一周和所有文章数
一个文章系统,原来是统计每个管理员发文章的总数
我用的是下面的SQL
SELECT top 7 count(articleid) as Counts,Editor
FROM Admin INNER JOIN Article ON Admin.UserName = Article.Editor
where Article.Passed=True
group by Editor
order by count(articleid) desc
显示效果:
用 户 排 行
名次 用户名 文章总数
1 bobo 20
2 xixi 10
现在要把名次的地方换成该用户最近一周的发文章数,效果如下:
用户名 最近一周 文章总数
bobo 4 20
xixi 6 10
用下面的SQL可以实现查询最近一周每个用户的发文章数
SELECT top 7 count(articleid) as Counts,Editor
FROM Admin INNER JOIN Article ON Admin.UserName = Article.Editor
where Article.Passed=True and DateDiff( 'd ',UpdateTime,#2007-3-21#) <7
group by Editor
order by count(articleid) desc
可是怎么用一条语句实现?
-------------------------------
斑竹们给的办法
SELECT
top 7 count(articleid) as Counts,
Editor ,
(select count(*) from Article where Editor=b.Editor and Passed=True)
FROM Admin as a
INNER JOIN Article as b ON a.UserName = b.Editor
where b.Passed=True
and DateDiff( 'd ',UpdateTime,#2007-3-21#) <7
group by Editor
order by count(articleid) desc
-----------------------------------
上面的语句有个小问题,就是如果一个用户总共发过10篇文章,可最近一周没有发过文章,查询的结果中就没有了这个用户
这个怎么解决,
我想的是,有了下面这个排行
名次 用户名 文章总数
1 bobo 20
2 xixi 10
在把该排行中存在的用户名的最近一周的发文章数查询出来
------解决方案--------------------select * from (
select UserName,nz((select count(*) from Article where a.[UserName]=Editor),0) as allcounts ,
nz((select count(*) from Article where a.[UserName]=Editor and datediff( 'd ',UpdateTime,date()) <7
),0) as weekcounts from Admin a) order by allcounts