mssql数据库asp排行统计数据(按月份查询排行)
表biao主键id上班时间字段Time员工号字段usid数量字段cont12011-9-1410:51:071086022011-9-1418:53:3420279320...
表biao
主键id 上班时间字段 Time 员工号字段usid 数量字段cont
1 2011-9-14 10:51:07 108 60
2 2011-9-14 18:53:34 202 79
3 2011-9-14 18:53:34 108 82
现在要统计本月数量cont值按最多的员工排行
输出本月
第一名:工号108 数量2208
第二名:工号102 数量2007
第三名:工号214 数量1978
查看上月数据
求解 展开
主键id 上班时间字段 Time 员工号字段usid 数量字段cont
1 2011-9-14 10:51:07 108 60
2 2011-9-14 18:53:34 202 79
3 2011-9-14 18:53:34 108 82
现在要统计本月数量cont值按最多的员工排行
输出本月
第一名:工号108 数量2208
第二名:工号102 数量2007
第三名:工号214 数量1978
查看上月数据
求解 展开
2个回答
展开全部
declare @currMonth as DateTime
declare @prevMonth as DateTime
set @currMonth = cast(cast(year(getdate()) as varchar(4)) + right('0'+cast(month(getdate()) as varchar (2)),2) + '01' as DateTime);
set @prevMonth = DATEADD(month,-1,@currMonth)
--本月
select usid, SUM(cont) as total_cont from biao where [Time] >= @currMonth and [Time] < DATEADD(month,1,@currMonth)
group by usid
order by total_cont desc
--上月
select usid, SUM(cont) as total_cont from biao where [Time] >= @prevMonth and [Time] < @currMonth
group by usid
order by total_cont desc
如果希望用一列专门显示名次,可以使用mssql 2005提供的rank()函数。例如:本月可以这么写
select rank() over(order by sum(cont) desc) as rnk,usid, SUM(cont) as total_cont from biao where [Time] >= @currMonth and [Time] < DATEADD(month,1,@currMonth)
group by usid
order by total_cont desc
declare @prevMonth as DateTime
set @currMonth = cast(cast(year(getdate()) as varchar(4)) + right('0'+cast(month(getdate()) as varchar (2)),2) + '01' as DateTime);
set @prevMonth = DATEADD(month,-1,@currMonth)
--本月
select usid, SUM(cont) as total_cont from biao where [Time] >= @currMonth and [Time] < DATEADD(month,1,@currMonth)
group by usid
order by total_cont desc
--上月
select usid, SUM(cont) as total_cont from biao where [Time] >= @prevMonth and [Time] < @currMonth
group by usid
order by total_cont desc
如果希望用一列专门显示名次,可以使用mssql 2005提供的rank()函数。例如:本月可以这么写
select rank() over(order by sum(cont) desc) as rnk,usid, SUM(cont) as total_cont from biao where [Time] >= @currMonth and [Time] < DATEADD(month,1,@currMonth)
group by usid
order by total_cont desc
更多追问追答
追问
数量值怎么输出显示?"&rs("cont")&"这样吗?
追答
是的。输出时只要做简单的字符串连就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select userid, sum(cont) from biao group by usid order by count(cont) where time bewteen 2011-8-31 and 2011-10-1
追问
数量值输出"&rs("cont")&"这样显示出错,请问怎样解决?才发现你这个是统计上班次数,不是以cont值排行的
追答
select userid, sum(cont) as total from biao group by usid order by sum(cont) where time bewteen 2011-8-31 and 2011-10-1
输出用"&rs("total")&"
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询