麻烦大家:用游标和存储过程写个求平均数问题,并寸到另一个表里面;基本要求是:@d1-->数据,@d2-->标志位,@d3-->取得数据的时间.标志位相同的数据相加,求平均后存到另个表里面,并把此标志位对应数据的最后时间保存下来.
例如:数据 标志位 时间
23 1 34
53 1 45
43 1 64
31 2 78
32 2 80
43 2 82
45 2 84
转化到另个表里是:
(平均值) 1 64
(平均值) 2 84 谢谢大家~~~
例如:数据 标志位 时间
23 1 34
53 1 45
43 1 64
31 2 78
32 2 80
43 2 82
45 2 84
转化到另个表里是:
(平均值) 1 64
(平均值) 2 84 谢谢大家~~~
from tablename
group by 标志位--插入语句
insert table1 (数据,标志位,时间)
select avg(数据) as 数据,标志位,max(时间) as 时间
from tablename
group by 标志位
insert newtable
select avg(数据) as 数据,标志位,max(时间) as 时间
from tablename
这样就可以了的!
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[mn]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[mn]
GOCREATE TABLE [dbo].[mn] (
[sd] [float] NULL ,
[st] [int] NULL
) ON [PRIMARY]
GO
以上是表
下面是存储过程:
CREATE PROCEDURE [update_mn_1]
(
@d1 float,
@d2 int
)
AS
DECLARE @n integer,
@i int
DECLARE youbiao1 CURSOR SCROLL
FOR
SELECT * from mn
SET @n=1
OPEN youbiao1
FETCH NEXT FROM youbiao1 INTO @d1,@d2WHILE @@FETCH_STATUS=0
BEGINIF(@d1=6)
SET @n=@n+1IF(@n%2!=0)
BEGIN
SET @i=1
WHILE(@i<=12)
BEGIN
IF(@d1>=1 and @d1<=3)
BEGIN
UPDATE mn
SET st=@i
WHERE sd =@d1
END
IF(@d1<=5 and @d1>=4)
CONTINUE
SET @i=@i+1
END
END
ELSE IF(@n%2=0)
BEGIN
SET @i=12
WHILE(@i>=1)
BEGIN
IF(@d1>=1 and @d1<=3)
BEGIN
UPDATE mn
SET st=@i
WHERE sd =@d1
END
IF(@d1<=5 and @d1>=4)
CONTINUE
SET @i=@i-1
END
END
FETCH NEXT FROM youbiao1 INTO @d1,@d2
ENDCLOSE youbiao1
DEALLOCATE youbiao1GO
我是想把属于同一块数据加上标志位,由于数据是对材料的来回扫描得来的所以奇数(++)表示从上往下扫描,偶数(--)表示从下往上,来更新标志位数据归类以后再求的平均数
if object_id('tempdb..#tmp') is not null
drop table #tmp
GO
----创建测试数据
declare @t table(数据 int,标志位 int,时间 int)
insert @t
select 23, 1, 34 union all
select 53, 1, 45 union all
select 43, 1, 64 union all
select 31, 2, 78 union all
select 32, 2, 80 union all
select 43, 2, 82 union all
select 45, 2, 84 union all
select 43, 1, 90 union all
select 54, 1, 92 union all
select 56, 1, 96 union all
select 63, 2, 98 union all
select 23, 2, 100----生成分组临时表
select *,0 as GroupId into #tmp from @t----生成分组ID
declare @GroupId int,@标志位 int
set @GroupId = 0
UPDATE #tmp SET
@标志位 = case when @标志位 is not null and @标志位 <> 标志位 then NULL else 标志位 end,
@GroupId = case when @标志位 is null then @GroupId + 1 else @GroupId end,
GroupId = @GroupId----分组汇总
select avg(数据) as 数据,min(标志位) as 标志位,max(时间) as 时间 from #tmp group by GroupId/*----将分组汇总结果插入到新表中
insert tb(平均值,标志位,时间)
select avg(数据) as 数据,min(标志位) as 标志位,max(时间) as 时间 from #tmp group by GroupId
*/----清除测试环境
drop table #tmp/*结果
数据 标志位 时间
------------------------------------------
39 1 64
37 2 84
51 1 96
43 2 100
*/
if object_id('tempdb..#tmp') is not null
drop table #tmp
GO
----创建测试数据
declare @t table(数据 int,时间 int,标志位 int)
insert @t
select 23, 34, 1 union all
select 53, 45, 1 union all
select 43, 64, 1 union all
select 31, 78, 2 union all
select 32, 80, 2 union all
select 43, 82, 2 union all
select 45, 84, 2 union all
select 43, 90, 1 union all
select 54, 92, 1 union all
select 56, 96, 1 union all
select 63, 98, 2 union all
select 23, 100, 2
----创建保存汇总结果的表变量
declare @tbAvgData table(平均值 int,时间 int,标志位 int)----生成分组临时表
select *,0 as GroupId into #tmp from @t----生成分组ID
declare @GroupId int,@标志位 int
set @GroupId = 0
UPDATE #tmp SET
@标志位 = case when @标志位 is not null and @标志位 <> 标志位 then NULL else 标志位 end,
@GroupId = case when @标志位 is null then @GroupId + 1 else @GroupId end,
GroupId = @GroupId----将分组汇总结果保存到指定的表中!!!
insert @tbAvgData(平均值,时间,标志位)
select avg(数据) as 数据,max(时间) as 时间,min(标志位) as 标志位 from #tmp group by GroupId----查询汇总结果
select * from @tbAvgData----清除测试环境
drop table #tmp/*结果
数据 时间 标志位
------------------------------------------
39 64 1
37 84 2
51 96 1
43 100 2
*/
insert @tbAvgData(平均值,时间,标志位)
select avg(数据) as 数据,max(时间) as 时间,min(标志位) as 标志位 from #tmp group by GroupId