CREATE TABLE [dbo].[node_data] ( [data_id] [bigint] IDENTITY (1, 1) NOT NULL , --//序号,主键 [node_id] [varchar] (16) COLLATE Chinese_PRC_CI_AS NULL , --//测点编号 [rp_date] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL , --//检测日期 [rp_time] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL , --//检测时间 [rp_wend] [decimal](18, 3) NULL , -- //温度 [rp_sid] [decimal](18, 3) NULL , --//湿度 ) ON [PRIMARY] insert node_data select '01','2004-01-01','',10,0 union all select '01','2004-01-02','',10,0 union all select '01','2004-02-01','',11,0 union all select '02','2004-02-02','',12,0 GO--查询日期的函数 create function f_mm( @value [decimal](18, 3), --要合计的日期的值 @ym datetime --要查询的年月 )returns varchar(8000) as begin declare @r varchar(8000) set @r='' select @r=@r+','+rp_date from( select distinct rp_date=cast(day(rp_date) as varchar) from node_data where rp_wend=@value and datediff(month,rp_date,@ym)=0 )a return(stuff(@r,1,1,'')) end godeclare @year char(4) set @year='2004' --要查询的年份select b ,[1月]=max([1月]) ,[2月]=max([2月]) from( select b.a,b.b ,[1月]=case when month(rp_date)=1 then case b.a when 1 then cast(avg(a.rp_wend) as varchar) when 2 then cast(max(a.rp_wend) as varchar) when 3 then dbo.f_mm(max(a.rp_wend),@year+'-1-1') end end ,[2月]=case when month(rp_date)=2 then case b.a when 1 then cast(avg(a.rp_wend) as varchar) when 2 then cast(max(a.rp_wend) as varchar) when 3 then dbo.f_mm(max(a.rp_wend),@year+'-2-1') end end from node_data a,( select a=1,b='月平均' union all select 2,'月最高值' union all select 3,'最高值日期' -- union all select 4,'月最低值' -- union all select 5,'最低值日期' )b where left(rp_date,4)=@year group by b.a,b.b,month(a.rp_date) )a group by a,b go--删除测试 drop table node_data drop function f_mm/*--测试结果b 1月 2月 ---------- ------------------ --------------- 月平均 10.000000 11.500000 月最高值 10.000 12.000 最高值日期 1,2 2(所影响的行数为 3 行)--*/
大致上的处理如下,我只写了两个月,其他月份照写,如果也要求最低值(看了你原来的帖子),函数的调用方式改为 dbo.f_mm(min(a.rp_wend),@year+'-1-1') 至少前面部分的每天的数据,因为你原来已经得到的处理方法,所以就不再写. 你只需要把这个结果与前面部分用union all 合并就行了,注意因为你的日期可以是多个日期的合并,所以是字符型,因为合并的时候要注意把你以前统计的转换成字符型.
用户应可选择统计的年份.
[data_id] [bigint] IDENTITY (1, 1) NOT NULL , --//序号,主键
[node_id] [varchar] (16) COLLATE Chinese_PRC_CI_AS NULL , --//测点编号
[rp_date] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL , --//检测日期
[rp_time] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL , --//检测时间
[rp_wend] [decimal](18, 3) NULL , -- //温度
[rp_sid] [decimal](18, 3) NULL , --//湿度
) ON [PRIMARY]
insert node_data select '01','2004-01-01','',10,0
union all select '01','2004-01-02','',10,0
union all select '01','2004-02-01','',11,0
union all select '02','2004-02-02','',12,0
GO--查询日期的函数
create function f_mm(
@value [decimal](18, 3), --要合计的日期的值
@ym datetime --要查询的年月
)returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+','+rp_date
from(
select distinct rp_date=cast(day(rp_date) as varchar)
from node_data
where rp_wend=@value
and datediff(month,rp_date,@ym)=0
)a
return(stuff(@r,1,1,''))
end
godeclare @year char(4)
set @year='2004' --要查询的年份select b
,[1月]=max([1月])
,[2月]=max([2月])
from(
select b.a,b.b
,[1月]=case when month(rp_date)=1
then case b.a
when 1 then cast(avg(a.rp_wend) as varchar)
when 2 then cast(max(a.rp_wend) as varchar)
when 3 then dbo.f_mm(max(a.rp_wend),@year+'-1-1')
end
end
,[2月]=case when month(rp_date)=2
then case b.a
when 1 then cast(avg(a.rp_wend) as varchar)
when 2 then cast(max(a.rp_wend) as varchar)
when 3 then dbo.f_mm(max(a.rp_wend),@year+'-2-1')
end
end
from node_data a,(
select a=1,b='月平均'
union all select 2,'月最高值'
union all select 3,'最高值日期'
-- union all select 4,'月最低值'
-- union all select 5,'最低值日期'
)b where left(rp_date,4)=@year
group by b.a,b.b,month(a.rp_date)
)a group by a,b
go--删除测试
drop table node_data
drop function f_mm/*--测试结果b 1月 2月
---------- ------------------ ---------------
月平均 10.000000 11.500000
月最高值 10.000 12.000
最高值日期 1,2 2(所影响的行数为 3 行)--*/
dbo.f_mm(min(a.rp_wend),@year+'-1-1')
至少前面部分的每天的数据,因为你原来已经得到的处理方法,所以就不再写.
你只需要把这个结果与前面部分用union all 合并就行了,注意因为你的日期可以是多个日期的合并,所以是字符型,因为合并的时候要注意把你以前统计的转换成字符型.