求sql 或存储过程,谢谢各位大侠

打酱油的无证程序猿 2014-01-13 04:01:57
TheTime iType score
2013-12-12 16:15:00.000 1 5.5
2013-12-12 16:23:00.000 1 5.7
2013-12-12 17:24:00.000 1 5.5
2013-12-12 17:06:00.000 2 5.9
2013-12-12 17:07:00.000 2 5.5

想要的统计结果

itype 1-2点 2-3点 。。 16-17点。。 17-18 23点到 0 点,0点到1点
------------------------------------------------------------------------
1 0 0 5.5+5.7+5.5 0 0
2 0 0 0 5.9+5.5 0



就是统计 itype 一天24个时间段的每个时间段score之和
...全文
154 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
谢谢大侠,非常感谢。。。
LongRui888 2014-01-13
  • 打赏
  • 举报
回复
把sum改成avg,你试试:
--drop table tb

create table tb(TheTime datetime,  	       iType int,	score  numeric(10,1))

insert into tb    
select '2013-12-12 16:15:00.000',	1	,5.5	union all
select '2013-12-12 16:23:00.000',	1	,5.7	union all
select '2013-12-12 17:24:00.000',	1	,5.5	union all
select '2013-12-12 17:06:00.000',	2	,5.9	union all
select '2013-12-12 17:07:00.000',	2	,5.5
go


declare @sql varchar(8000)

set @sql = ''


select @sql = @sql + ',avg(case when substring(convert(varchar(20),TheTime,120),12,2)>='+CAST(number%24 as varchar)+
                     ' and substring(convert(varchar(20),TheTime,120),12,2)<'+cAST((number+1)%24 as varchar)+
                     ' then score else 0 end) as ['+CAST(number%24 as varchar)+'-'+CAST((number+1)%24as varchar)+'点]'
from master..spt_values 
where type = 'P' and number >=1 and number <=24


set @sql = 'select itype'+@sql +       
		   ' from tb 
			group by itype'

exec( @sql)
/*
itype	1-2点	2-3点	3-4点	4-5点	5-6点	6-7点	7-8点	8-9点	9-10点	10-11点	11-12点	12-13点	13-14点	14-15点	15-16点	16-17点	17-18点	18-19点	19-20点	20-21点	21-22点	22-23点	23-0点	0-1点
1	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	3.733333	1.833333	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000
2	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	5.700000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000	0.000000
*/
LongRui888 2014-01-13
  • 打赏
  • 举报
回复
引用 8 楼 cnlmgsoft 的回复:
谢谢楼上大侠啊 这个是求和的报表 如果是求平均值 好改吗 2013-12-12 16:15:00.000 1 5.5 2013-12-12 16:23:00.000 1 5.7 16点2行数据 ,就平均值 就是 (5.5+5.7)/2 16点如果有3行数据 除以3 这个就平均值 好改吗 谢谢楼上大侠啊 再帮俺一次吧
好改,只要把sum改成avg就行了:

--drop table tb

create table tb(TheTime datetime,  	       iType int,	score  numeric(10,1))

insert into tb    
select '2013-12-12 16:15:00.000',	1	,5.5	union all
select '2013-12-12 16:23:00.000',	1	,5.7	union all
select '2013-12-12 17:24:00.000',	1	,5.5	union all
select '2013-12-12 17:06:00.000',	2	,5.9	union all
select '2013-12-12 17:07:00.000',	2	,5.5
go


declare @sql varchar(8000)

set @sql = ''


select @sql = @sql + ',avg(case when substring(convert(varchar(20),TheTime,120),12,2)>='+CAST(number%24 as varchar)+
                     ' and substring(convert(varchar(20),TheTime,120),12,2)<'+cAST((number+1)%24 as varchar)+
                     ' then score else 0 end) as ['+CAST(number%24 as varchar)+'-'+CAST((number+1)%24as varchar)+'点]'
from master..spt_values 
where type = 'P' and number >=1 and number <=24


set @sql = 'select itype'+@sql +       
		   ' from tb 
			group by itype'

exec( @sql)
/*
itype	1-2点	2-3点	3-4点	4-5点	5-6点	6-7点	7-8点	8-9点	9-10点	10-11点	11-12点	12-13点	13-14点	14-15点	15-16点	16-17点	17-18点	18-19点	19-20点	20-21点	21-22点	22-23点	23-0点	0-1点
1	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	11.2	5.5	0.0	0.0	0.0	0.0	0.0	0.0	0.0
2	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	11.4	0.0	0.0	0.0	0.0	0.0	0.0	0.0
*/
  • 打赏
  • 举报
回复
谢谢楼上大侠啊 这个是求和的报表 如果是求平均值 好改吗 2013-12-12 16:15:00.000 1 5.5 2013-12-12 16:23:00.000 1 5.7 16点2行数据 ,就平均值 就是 (5.5+5.7)/2 16点如果有3行数据 除以3 这个就平均值 好改吗 谢谢楼上大侠啊 再帮俺一次吧
發糞塗牆 2014-01-13
  • 打赏
  • 举报
回复
你这个只统计一天是吧?数据会不会还有其他月份?sqlserver多少?
LongRui888 2014-01-13
  • 打赏
  • 举报
回复
修改了一下:

--drop table tb

create table tb(TheTime datetime,  	       iType int,	score  numeric(10,1))

insert into tb    
select '2013-12-12 16:15:00.000',	1	,5.5	union all
select '2013-12-12 16:23:00.000',	1	,5.7	union all
select '2013-12-12 17:24:00.000',	1	,5.5	union all
select '2013-12-12 17:06:00.000',	2	,5.9	union all
select '2013-12-12 17:07:00.000',	2	,5.5
go


declare @sql varchar(8000)

set @sql = ''


select @sql = @sql + ',SUM(case when substring(convert(varchar(20),TheTime,120),12,2)>='+CAST(number%24 as varchar)+
                     ' and substring(convert(varchar(20),TheTime,120),12,2)<'+cAST((number+1)%24 as varchar)+
                     ' then score else 0 end) as ['+CAST(number%24 as varchar)+'-'+CAST((number+1)%24as varchar)+'点]'
from master..spt_values 
where type = 'P' and number >=1 and number <=24


set @sql = 'select itype'+@sql +       
		   ' from tb 
			group by itype'

exec( @sql)
/*
itype	1-2点	2-3点	3-4点	4-5点	5-6点	6-7点	7-8点	8-9点	9-10点	10-11点	11-12点	12-13点	13-14点	14-15点	15-16点	16-17点	17-18点	18-19点	19-20点	20-21点	21-22点	22-23点	23-0点	0-1点
1	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	11.2	5.5	0.0	0.0	0.0	0.0	0.0	0.0	0.0
2	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	11.4	0.0	0.0	0.0	0.0	0.0	0.0	0.0
*/
LongRui888 2014-01-13
  • 打赏
  • 举报
回复


create table tb(TheTime datetime,  	       iType int,	score  numeric(10,1))

insert into tb    
select '2013-12-12 16:15:00.000',	1	,5.5	union all
select '2013-12-12 16:23:00.000',	1	,5.7	union all
select '2013-12-12 17:24:00.000',	1	,5.5	union all
select '2013-12-12 17:06:00.000',	2	,5.9	union all
select '2013-12-12 17:07:00.000',	2	,5.5
go


declare @sql varchar(8000)

set @sql = ''


select @sql = @sql + ',SUM(case when substring(convert(varchar(20),TheTime,120),12,2)>='+CAST(number as varchar)+
                     ' and substring(convert(varchar(20),TheTime,120),12,2)<'+cAST(number+1 as varchar)+
                     ' then score else 0 end) as ['+CAST(number as varchar)+'-'+CAST(number+1 as varchar)+'点]'
from master..spt_values 
where type = 'P' and number >=1 and number <=24


set @sql = 'select itype'+@sql +       
		   ' from tb 
			group by itype'

exec( @sql)
/*
itype	1-2点	2-3点	3-4点	4-5点	5-6点	6-7点	7-8点	8-9点	9-10点	10-11点	11-12点	12-13点	13-14点	14-15点	15-16点	16-17点	17-18点	18-19点	19-20点	20-21点	21-22点	22-23点	23-24点	24-25点
1	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	11.2	5.5	0.0	0.0	0.0	0.0	0.0	0.0	0.0
2	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	11.4	0.0	0.0	0.0	0.0	0.0	0.0	0.0
*/
  • 打赏
  • 举报
回复
版主大侠帮忙啊。。。
  • 打赏
  • 举报
回复
问题写错了,这个是结果集合 itype 1-2点 2-3点 。。 16-17点。。 17-18 23点到 0 点,0点到1点 ------------------------------------------------------------------------ 1 0 0 5.5+5.7 5.5 0 2 0 0 0 5.9+5.5 0
  • 打赏
  • 举报
回复
告诉别人这样计算 是求和
發糞塗牆 2014-01-13
  • 打赏
  • 举报
回复
你那个5.5+5.7+5.5是要展示的还是告诉别人要这样计算的?

34,837

社区成员

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

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