求SQL语句 查出本月的会员积分排名,

wangrutie 2006-07-12 09:59:32
求SQL语句 查出本月的会员积分排名,

比如数据是

用户名 积分 时间
aaa 2 2006-7-9 13:00:12
bbb 3 2006-7-6 13:00:12
ccc 3 2006-7-4 13:00:12
aaa 5 2006-7-9 13:00:12
ccc 1 2006-7-4 13:00:12

查到的结果是
用户名 积分
aaa 7
ccc 4
bbb 3
...全文
626 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
paoluo 2006-07-12
  • 打赏
  • 举报
回复
wangrutie(wangrutie) ( ) 信誉:100 2006-07-12 11:07:00 得分: 0


积分有负的结果会不对吗?等会结帐


--------------
如果“积分有负”,這個积分是否還是需要加上,如果是的話,語句就不用改。
wangrutie 2006-07-12
  • 打赏
  • 举报
回复
积分有负的结果会不对吗?等会结帐
zlp321002 2006-07-12
  • 打赏
  • 举报
回复
--的确没考虑年.: )
declare @t table(用户名 varchar(20),积分 int,时间 datetime)
insert into @t select 'aaa',2,'2006-7-9 13:00:12'
union all select 'bbb',3,'2006-7-6 13:00:12'
union all select 'ccc',3,'2006-7-4 13:00:12'
union all select 'aaa',5,'2006-7-9 13:00:12'
union all select 'ccc',1,'2006-7-4 13:00:12'
union all select 'ccc',1,'2007-7-4 13:00:12'

select 用户名=用户名,积分=sum(积分)
from @t
where datediff(month,时间,{fn now()})=0 and datediff(year,时间,{fn now()})=0
group by 用户名
order by sum(积分) desc

/*
用户名 积分
-------------------- -----------
aaa 7
ccc 4
bbb 3

(3 行受影响)
*/
paoluo 2006-07-12
  • 打赏
  • 举报
回复
fcuandy(要学的东西还很多) ,沒有將年考慮進去
fcuandy 2006-07-12
  • 打赏
  • 举报
回复
SELECT 用户名,SUM(积分) 总积分
FROM 表
WHERE DATEDIFF(mm,时间,GETDATE())=0
GROUP BY 用户名
ORDER BY 总积分
paoluo 2006-07-12
  • 打赏
  • 举报
回复
Create Table TEST
(用户名 Varchar(10),
积分 Int,
时间 DateTime)
Insert TESt Select 'aaa', 2, '2006-7-9 13:00:12'
Union All Select 'bbb', 3, '2006-7-6 13:00:12'
Union All Select 'ccc', 3, '2006-7-4 13:00:12'
Union All Select 'aaa', 5, '2006-7-9 13:00:12'
Union All Select 'ccc', 1, '2006-7-4 13:00:12'
GO
Select
用户名,
SUM(积分) As 积分
From TEST
Where Convert(Varchar(7),时间,120)=Convert(Varchar(7),GetDate(),120)
Group By 用户名
Order By 积分 Desc
GO
Drop Table TEST
--Result
/*
用户名 积分
aaa 7
ccc 4
bbb 3
*/
tangqijun199 2006-07-12
  • 打赏
  • 举报
回复
select 用户,sum(积分) from tablename where 时间 between '2006-07-01' and '2006-08-01' group by 用户 order by sum(积分) desc
tangqijun199 2006-07-12
  • 打赏
  • 举报
回复
select 用户,sum(积分) from tablename where 时间 between '2006-07-01' and '2006-08-01' group by 用户
zlp321002 2006-07-12
  • 打赏
  • 举报
回复
select 用户名=用户名,积分=sum(积分)
from 表
where datediff(month,时间,{fn now()})=0

group by 用户名

order by sum(积分) desc
paoluo 2006-07-12
  • 打赏
  • 举报
回复
Select
用户名,
SUM(积分) As 积分
From TableName
Where Convert(Varchar(7),时间,120)=Convert(Varchar(7),GetDate(),120)
Group By 用户名
Order By 积分 Desc
paoluo 2006-07-12
  • 打赏
  • 举报
回复
暈一下
jetdw 2006-07-12
  • 打赏
  • 举报
回复
select 用户名,sum(积分) as 积分
from tb
where
substring(convert(varchar,时间,120),6,1)=substring(convert(varchar,getdate(),120),7,1)
group by 用户名
YANG_1994 2006-07-12
  • 打赏
  • 举报
回复
还是用CONVERTConvert(Varchar(7),时间,120)考虑的比较全面
lywf 2006-07-12
  • 打赏
  • 举报
回复
select 用户名,sum(积分) as 积分
from <tablename>
where datepart(month,时间) = datepart(month,getdate())
group by 用户名
order by sum(积分) desc
paoluo 2006-07-12
  • 打赏
  • 举报
回复
Teng_s2000() ( ) 信誉:100 2006-07-12 15:20:00 得分: 0


select username,sum(score),month(lastdate) from test
group by username,month(lastdate)
order by sum(score) desc
这个sql我在sql2005上测试通过,能满足要求吗


-------------
都說了這麼寫沒考慮年份
Teng_s2000 2006-07-12
  • 打赏
  • 举报
回复
select username,sum(score),month(lastdate) from test
group by username,month(lastdate)
order by sum(score) desc
这个sql我在sql2005上测试通过,能满足要求吗
kisa99 2006-07-12
  • 打赏
  • 举报
回复
select 用户,sum(积分) from tablename where 时间 >= '2006-07-01' and 时间<'2006-08-01' group by 用户 order by sum(积分) desc

34,597

社区成员

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

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