100分在線求一SQL語句
現有一表如下 取的日期范圍06-12-15到07-01-14
日期 姓名 得分
2006-12-15 A 10
. A 20
. A
. A
2007-01-14 A
2006-12-15 B
. B
. B
2007-01-14 B
. C
. C
. C
. C
. D
. .
現求一語句或簡單的實現方法得到下面的結果將每周的得分匯總
姓名 開始日期(周開頭﹕日) 結束日期(周結尾﹕六) 一周得分和
A 2006-12-15 2006-12-16
A 2006-12-17 2006-12-23
..
B 2006-12-15 2006-12-16
..
..
謝謝
------解决方案--------------------select 姓名,cast(datepart(year,dateadd(day,-1,日期)) as varchar(4))+cast(datepart(week,dateadd(day,-1,日期)) as varchar(2)) as 年周,
sum(得分) as 得分 from tb
group by cast(datepart(year,dateadd(day,-1,日期)) as varchar(4))+cast(datepart(week,dateadd(day,-1,日期)) as varchar(2)),姓名
----
不好意思,写得太快了,再扩展一下就好了
------解决方案--------------------create table tb
(
t_date datetime,
t_week int
)
declare @s_date datetime,@e_date as datetime,@t_date as datetime,@i int,@j int
set @s_date = cast( '20061215 ' as datetime)
set @t_date = cast( '20061215 ' as datetime)
set @e_date = cast( '20070114 ' as datetime)
set @i = 1
set @j = 0