62,046
社区成员
发帖
与我相关
我的任务
分享
WITH A AS(
SELECT B.[BumenName] AS [部门名称],COUNT(*) AS [文章总数],ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) AS 排名 FROM [News] AS N
INNER JOIN [Bumen] AS B
ON N.[BumenID] = B.[BumenID]
GROUP BY B.[BumenName]
)SELECT * FROM A
ORDER BY [文章总数] DESC
SELECT COUNT(*) FROM [News] WHERE DATEPART(yy,AddTime)=DATEPART(yy,GETDATE()) AND [BumenID]=1
SELECT COUNT(*) FROM [News] WHERE DATEPART(yy,AddTime)=DATEPART(yy,GETDATE()) AND DATEPART(mm,AddTime)=DATEPART(mm,GETDATE()) [BumenID]=1
SELECT COUNT(*) FROM [News] WHERE DATEPART(yy,AddTime)=DATEPART(yy,GETDATE()) AND DATEPART(mm,AddTime)=DATEPART(mm,GETDATE()) AND DATEPART(dd,AddTime)=DATEPART(dd,GETDATE()) [BumenID]=1
--排序加上
order by t.num desc
--加上排名
select
r.BumenName as 部门名称,
t.num as 文章总数,
(select count(1) from News where BumenID=t.BumenID and year(AddTime)= year(getdate())) as 本年数量,
(select count(1) from News where BumenID=t.BumenID and datediff(month,AddTime,getdate()) = 1) as 上月数量,
(select count(1) from News where BumenID=t.BumenID and datediff(month,AddTime,getdate()) = 0) as 本月数量,
(select count(1) from News where BumenID=t.BumenID and datediff(day,AddTime,getdate()) = 0) as 当天数量,
t.排名
from
(select BumenID,num,(select count(1) from (
select BumenID,count(1) as num from News
group by BumenID
) g where num<=h.num) as 排名
from
(
select BumenID,count(1) as num from News
group by BumenID
) h ) t
join Bumen r
on t.BumenID=r.BumenID
select
r.BumenName as 部门名称,
count(t.NewID) as 文章总数,
(select count(1) from News where BumenID=t.BumenID and year(AddTime)= year(getdate())) as 本年数量,
(select count(1) from News where BumenID=t.BumenID and datediff(month,AddTime,getdate()) = 1) as 上月数量,
(select count(1) from News where BumenID=t.BumenID and datediff(month,AddTime,getdate()) = 0) as 本月数量,
(select count(1) from News where BumenID=t.BumenID and datediff(day,AddTime,getdate()) = 0) as 当天数量
from News t join Bumen r
on t.BumenID=r.BumenID
group by r.BumenName