自定义函数实现字符串中数据的运算和统计2

上一篇是汇总操作,这一篇是求平均值,并且数据可能出现非数字和负数

问题:
数据表如下

cate            values                                  dates
 A1           12.32,15.6,14.5,45.00,78.12,23.45         2006-04-01
 A2           14.56,67.23,45.12,23.1,34.67,56.98        2006-04-01
 A1           12.31,15.6,14.5,44.00,78.12,23.45         2006-04-01
 A2           14.56,67.23,45.12,23.1,34.67,56.98        2006-04-01
 A1           12.32,15.6,14.5,45.00,78.12,23.45         2006-05-01
 A2           14.56,67.23,45.12,23.1,34.67,56.98        2006-05-01
 A1           12.31,15.6,14.5,44.00,78.12,23.45         2006-05-01
 A2           14.56,67.23,45.12,23.1,34.67,56.98        2006-05-01
 A1           -12.32,15.6,14.5,45.00,78.12,23.45         2006-04-01
 A2           12.32,-15.6,14.5,无,78.12,23.45           2006-04-01
不用存储过程实现如下数据统计
 cate            T1                                     dates
A1   4.10333,15.6,14.5,44.6667,78.12,23.45              2006-04-01
A1   12.315,15.6,14.5,44.5,78.12,23.45                  2006-05-01
A2   13.8133,39.62,34.9133,15.4,49.1533,45.8033         2006-04-01
A2   14.56,67.23,45.12,23.1,34.67,56.98                 2006-05-01
==========================================================================
就是 values 值之间用,隔开的,当cate和dates 相同时,算values中各个以,隔开的数值的平均值,要求是不用存储过程实现

实现:

--建立环境

create table up(
cate  varchar(3),
[values]  varchar(50),
dates datetime
)

insert up select
 'A1','12.32,15.6,14.5,45.00,78.12,23.45','2006-04-01'
union all select
 'A2','14.56,67.23,45.12,23.1,34.67,56.98','2006-04-01'
union all select
 'A1','12.31,15.6,14.5,44.00,78.12,23.45','2006-04-01'
union all select
 'A2','14.56,67.23,45.12,23.1,34.67,56.98','2006-04-01'
union all select
 'A1','12.32,15.6,14.5,45.00,78.12,23.45','2006-05-01'
union all select
 'A2','14.56,67.23,45.12,23.1,34.67,56.98','2006-05-01'
union all select
 'A1','12.31,15.6,14.5,44.00,78.12,23.45','2006-05-01'
union all select
 'A2','14.56,67.23,45.12,23.1,34.67,56.98','2006-05-01'
union all select
 'A1','-12.32,15.6,14.5,45.00,78.12,23.45','2006-04-01'
union all select
 'A2','12.32,-15.6,14.5,无,78.12,23.45','2006-04-01'

go

--求平均值函数
create function fn_ValuesAvg(
@cate  varchar(3),
@dates datetime
)
returns varchar(50)
as
begin
   declare @r varchar(50)
   set @r=''
   declare @t table (
      [values] varchar(50)
    )
   insert @t select [values] from up where cate=@cate and dates=@dates

   declare @t1 table (
      No int,
      [values] float
    )
   declare @No int

   set @No=1
   while exists (select 1 from @t where charindex(',',[values])>0)
   begin
        insert @t1 select @No,
             case when isnumeric(left([values],charindex(',',[values])-1))=1 then cast(left([values],charindex(',',[values])-1) as float) else 0 end
from @t

        update @t set [values]=stuff([values],1,charindex(',',[values]),'')
        set @No=@No+1
   end
   insert @t1 select @No,case when isnumeric([values])=1 then cast([values] as float) else 0 end from @t

   select @r=@r+','+cast(avg([Values]) as varchar) from @t1 group by No order by No

   return stuff(@r,1,1,'')
end
go

--查询
select cate,dbo.fn_valuesavg(cate,dates) as T,dates
from up
group by cate,dates

--结果

cate T                                                  dates                                                 
---- -------------------------------------------------- ------------------------------------------------------
A1   4.10333,15.6,14.5,44.6667,78.12,23.45              2006-04-01 00:00:00.000
A1   12.315,15.6,14.5,44.5,78.12,23.45                  2006-05-01 00:00:00.000
A2   13.8133,39.62,34.9133,15.4,49.1533,45.8033         2006-04-01 00:00:00.000
A2   14.56,67.23,45.12,23.1,34.67,56.98                 2006-05-01 00:00:00.000

(所影响的行数为 4 行)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值