日期:2014-05-17  浏览次数:20587 次

sql2000 按旬统计
表:A
字段:时间、得分1、得分2、得分3、人员名称、人员ID

表中数据

人员ID     名称    得分1   得分2   得分3    时间
123        张三     1       1       1         2012-05-01
123        张三     1       1       1         2012-05-02
......
123        张三     1       1       1         2012-05-31

321        李四     2       2       2         2012-05-01
321        李四     2       2       2         2012-05-02
......
321        李四     2       2       2         2012-05-31


我想要得出的结果是:
在这些数据中间插入 旬的统计 例如 5月10号 后就添加一条这样的记录
 123        张三     10       10       10         上旬

小弟现在没分了,等有分了一定补偿谢谢

------解决方案--------------------
declare @test table(id int, name nvarchar(4),score1 int, score2 int, score3 int, dt datetime)
insert into @test
select 123, N'张三', 1, 1, 1, '2012-05-01' union
select 123, N'张三', 1, 1, 1, '2012-05-03' union
select 123, N'张三', 1, 1, 1, '2012-05-12' union
select 123, N'张三', 1, 1, 1, '2012-05-24' union
select 123, N'张三', 1, 1, 1, '2012-05-28' union
select 123, N'张三', 1, 1, 1, '2012-05-31' union
select 123, N'李四', 1, 1, 1, '2012-06-01' union
select 123, N'李四', 2, 2, 2, '2012-06-13' union
select 123, N'李四', 1, 2, 1, '2012-06-12' union
select 123, N'李四', 1, 2, 2, '2012-06-15' union
select 123, N'李四', 1, 1, 2, '2012-06-22' union
select 123, N'李四', 1, 2, 1, '2012-06-30'

;with t as
(
select *,case when day(dt) between 1 and 10 then N'上旬' when day(dt) between 11 and 20 then N'中旬' else N'下旬' end dt_range from @test
)
select id,name,sum(score1) score1,sum(score2) score2,sum(score3) score3,convert(varchar(7),dt,120) yearMonth,dt_range from t
group by id,name,convert(varchar(7),dt,120),dt_range

/*
id       &n