循环计算”时、日、月、年“数据的方法思路

1、使用游标实现跨数据库导数据

参考存储过程:

ALTER proc [dbo].[Nsp_SiPingElectricityData]
as
declare @DANo Char(32)
declare @DATime Datetime
declare @LogTime Datetime
declare @MeterType Char(4)
declare @MeterNo Char(20)
declare @Qty Decimal(18,6)
declare @tagname nvarchar(50)
declare @sql nvarchar(800)

declare convertCursor cursor for
select replace(replace(replace(replace(convert(nvarchar(25),getdate(),121),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘),‘.‘,‘‘),
b.tagname,b.PValue ,b.date_time a,b.date_time ,‘EM‘ ,bb.meterno
from openrowset(‘SQLOLEDB‘,‘10.30.16.182‘;‘sa‘;‘tjdx‘,tj_siping2.dbo.Substation_siping) b,SiPingElectricityMeter bb
where
b.PValue>0
and
b.tagname=bb.TagNameNew
and
(b.label is null or b.label=0)
and b.Date_time is not null

order by b.Date_time asc
open convertCursor
fetch next from convertCursor into @DANo,@tagname,@QTY,@DATime,@LogTime,@MeterType,@meterno
while @@FETCH_STATUS=0
begin
begin
insert into SiPingElectricityData(DANo,DATime,LogTime,MeterType,Qty,Qty01,meterno,tagname) values(@DANo,@DATime,@LogTime,@MeterType,@Qty,@Qty,@meterno,@tagname)
begin
set @sql=‘Update openrowset(‘‘sqloledb‘‘,‘‘10.30.16.182‘‘;‘‘sa‘‘;‘‘tjdx‘‘,tj_siping2.dbo.Substation_siping) set label=1 where tagname=‘‘‘[email protected]+‘‘‘ and date_time=‘‘‘+convert(nvarchar(25),@DATime,21)+‘‘‘‘
exec (@sql)
end
end
fetch next from convertCursor into @DANo,@tagname,@QTY,@DATime,@LogTime,@MeterType,@meterno
end
close convertCursor
deallocate convertCursor
set nocount on

2、使用游标从表中循环读取数据,然后进行逐条处理

参考存储过程:

USE [CloudDatasCenter]
GO
/****** Object: StoredProcedure [dbo].[Usp_GetTanHouse_GSHP_Solar_DatasCompute] Script Date: 07/14/2014 15:18:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[Usp_GetTanHouse_GSHP_Solar_DatasCompute]
as
declare @DANo nvarchar(50)
declare @DATime datetime
declare @LogTime datetime
declare @MeterNo char(20)
declare @MeterType char(4)
declare @Unit char(10)
declare @RTQty decimal(18, 6)
declare @RTQtyOlder decimal(18, 6)
declare @ErrorFlag int
Declare @Collect int

Declare @IncreaseQty Decimal(18,6)
Declare @IncreaseQtyOlder Decimal(18,6)

declare @Qty decimal(18, 6)
declare @sql nvarchar(800)
declare @j int

declare convertCursor cursor for
select top 10000 replace(replace(replace(replace(convert(nvarchar(25),getdate(),121),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘),‘.‘,‘‘) DANo,thgsd.DATime, thgsd.LogTime,
thgsd.MeterNo,m.MeterType, thgsd.Qty,m.Unit
from TanHouse_GSHP_Solar_Datas thgsd left join meter m on thgsd.MeterNo=m.MeterNo
where m.Usable=1 and m.ProjectNo=‘TanHouse‘
and thgsd.DATime is not null
and thgsd.lable=0
--暂时排除为0数据 2011-12-08
--and qty>=‘0‘
and thgsd.Qty>0
order by thgsd.DATime asc

open convertCursor
fetch next from convertCursor into @DANo,@DATime,@LogTime,@MeterNo,@MeterType,@Qty,@Unit
while @@FETCH_STATUS=0
begin
begin

print CONVERT(varchar(100), @DATime, 121)
print @MeterNo
print @Qty

select @RTQty=RTQty,@RTQtyOlder=RTQtyOlder,@ErrorFlag=ErrorFlag,@Collect=Collect from Meter where [email protected]

if(@Collect=1)
begin

If IsNull(@Qty,0) = 0
Begin
Set @Qty = 0;
End

If IsNull(@RTQty,0) = 0
Begin
Set @RTQty = 0;
End

If IsNull(@RTQtyOlder,0) = 0
Begin
Set @RTQtyOlder = 0;
End

if @ErrorFlag=0
begin
--print ‘@ErrorFlag 0‘;
--print @ErrorFlag
--print ‘@Qty @RTQty‘;
--print @Qty
--print @RTQty
set @[email protected]@RTQty;

end
else
begin
--print ‘@ErrorFlag 1‘;

--print @ErrorFlag
--print ‘@Qty @RTQty @RTQtyOlder‘;
-- print @Qty
--print @RTQty
--print @RTQtyOlder
set @[email protected]@RTQty;
set @[email protected]@RTQtyOlder;

if @IncreaseQtyOlder>=0
begin
set @[email protected]
end
end

if @IncreaseQty>=0 ---如果增量不为负数
begin

--print ‘@IncreaseQty>=0‘;

--print @IncreaseQty
update Meter set [email protected],[email protected],ErrorFlag=0 where [email protected]
exec sp_DataComputer_TanHouse_GSHP_Solar_Datas @DANo,@DATime,@LogTime,@MeterType,@MeterNo,@IncreaseQty,@Unit;

--print CONVERT(varchar(100), GETDATE(), 121)+‘计算时间‘
--print ‘RTQty ,RTQtyOlder,ErrorFlag‘;
select @RTQty=RTQty, @RTQtyOlder=RTQtyOlder,@ErrorFlag=ErrorFlag from meter where [email protected]
--print @RTQty
--print @RTQtyOlder
--print @ErrorFlag

end
else
begin
--print ‘@IncreaseQty<0‘;

--print @IncreaseQty
update Meter set [email protected],ErrorFlag=1 where [email protected]

--print ‘RTQty ,RTQtyOlder,ErrorFlag‘;
select @RTQty=RTQty, @RTQtyOlder=RTQtyOlder,@ErrorFlag=ErrorFlag from meter where [email protected]
--print @RTQty
-- print @RTQtyOlder
-- print @ErrorFlag
end

end --IfEnd

else
begin

Declare @DAYear Char(4);
Declare @DAMonth Char(2);
Declare @DADay Char(2);
Declare @DAHour Char(2);

Set @DAYear = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,120))),1,4);
Set @DAMonth = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,120))),6,2);
Set @DADay = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,120))),9,2);
--Set @DADay = Ltrim(Rtrim(Convert(varchar(10),@DATime,112)));
Set @DAHour = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,114))),1,2);
--print @DADay
Insert TanHouseDatasByRealTime(DANo,DATime,LogTime,MeterType,MeterNo,Qty,DADay,DAHour)
Select @DANo,@DATime,@LogTime,@MeterType,@MeterNo,@Qty,(@[email protected][email protected]),@DAHour;

--print CONVERT(varchar(100), GETDATE(), 121)+‘计算时间‘
end --ElseEnd

update TanHouse_GSHP_Solar_Datas set lable=1 where [email protected] and [email protected];
-- print ‘--------汇总成功----------‘
--print ‘DATime=‘+convert(nvarchar(25),@DATime,120)

end
fetch next from convertCursor into @DANo,@DATime,@LogTime,@MeterNo,@MeterType,@Qty,@Unit
end
close convertCursor
deallocate convertCursor
set nocount on

3、使用事务,进行“时,日、月、年”的计算

参考存储过程:

USE [CloudDatasCenter]
GO
/****** Object: StoredProcedure [dbo].[sp_DataComputer_TanHouse_GSHP_Solar_Datas] Script Date: 07/14/2014 15:18:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[sp_DataComputer_TanHouse_GSHP_Solar_Datas]
@DANo Char(32),--20110112182345207插入时间
@DATime Datetime,--采集时间
@LogTime Datetime,--更新时间
@MeterType Char(4),--表具类型
@MeterNo Char(20),--表具号码
@Qty Decimal(18,6),--数值
@Unit Char(4),--单位
@outputpar int = 0 output--输出返回值
as

--带事务存储过程模板

begin
-- --print @MeterNo
-- Declare @Collect int;
-- select @Collect=Collect from Meter where [email protected]
-- --判断是否进行汇总计算
-- --print @Collect
----如果需要计算
--if(@Collect=1)
--begin

--开始事务,开始数据计算-------------------------------------------------------------------------------------------------------
BEGIN TRANSACTION
SAVE TRANSACTION sp_Datacomputer_TRANS

--事务内容,计算时日月数据.

Declare @DAYear Char(4);
Declare @DAMonth Char(2);
Declare @DADay Char(2);
Declare @DAHour Char(2);

Declare @RowCnt_Year Int;
Declare @RowCnt_Month Int;
Declare @RowCnt_Day Int;
Declare @RowCnt_Hour Int;

Set @DAYear = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,120))),1,4);
Set @DAMonth = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,120))),6,2);
Set @DADay = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,120))),9,2);
Set @DAHour = Substring(Ltrim(Rtrim(Convert(varchar(10),@DATime,114))),1,2);

--print @DAHour

Set @RowCnt_Year = (Select Count(*) From TanHouseEnergyDataSumByYear Where MeterNo = @MeterNo
And MeterType = @MeterType And DAYear = @DAYear);

If IsNull(@RowCnt_Year,0) = 0
Begin
Set @RowCnt_Year = 0;
End

Set @RowCnt_Month = (Select Count(*) From TanHouseEnergyDataSumByMonth Where MeterNo = @MeterNo
And MeterType = @MeterType And DAYear = @DAYear And DAMonth = @DAMonth);

If IsNull(@RowCnt_Month,0) = 0
Begin
Set @RowCnt_Month = 0;
End

Set @RowCnt_Day = (Select Count(*) From TanHouseEnergyDataSumByDay Where MeterNo = @MeterNo
And MeterType = @MeterType And DAYear = @DAYear And DAMonth = @DAMonth And DADay = @DADay );

If IsNull(@RowCnt_Day,0) = 0
Begin
Set @RowCnt_Day = 0;
End

Set @RowCnt_Hour = (Select Count(*) From TanHouseEnergyDataSumByHour Where MeterNo = @MeterNo
And MeterType = @MeterType And DADay = IsNull(@DAYear,‘‘) + IsNull(@DAMonth,‘‘) + IsNull(@DADay,‘‘)
And DAHour = @DAHour);

If IsNull(@RowCnt_Hour,0) = 0
Begin
Set @RowCnt_Hour = 0;
End

if (@@error <> 0)
begin
goto LABROLLBACK
end

--begin try
--begin tran
If @RowCnt_Year > 0
Begin
Update TanHouseEnergyDataSumByYear Set SumQty = SumQty + @Qty
Where MeterNo = @MeterNo And MeterType = @MeterType And DAYear = @DAYear ;
End
Else
Begin
Insert TanHouseEnergyDataSumByYear(MeterNo,MeterType,DAYear,SumQty,Unit)
Select @MeterNo,@MeterType,@DAYear,@Qty,@Unit;
End

if (@@error <> 0)
begin
goto LABROLLBACK
end

If @RowCnt_Month > 0
Begin
Update TanHouseEnergyDataSumByMonth Set SumQty = SumQty + @Qty
Where MeterNo = @MeterNo And MeterType = @MeterType And DAYear = @DAYear And DAMonth = @DAMonth;
End
Else
Begin
Insert TanHouseEnergyDataSumByMonth(MeterNo,MeterType,DAYear,DAMonth,SumQty,Unit)
Select @MeterNo,@MeterType,@DAYear,@DAMonth,@Qty,@Unit;
End

if (@@error <> 0)
begin
goto LABROLLBACK
end

If @RowCnt_Day > 0
Begin
Update TanHouseEnergyDataSumByDay Set SumQty = SumQty + @Qty
Where MeterNo = @MeterNo And MeterType = @MeterType And DAYear = @DAYear And DAMonth = @DAMonth And DADay = @DADay;
End
Else
Begin
Insert TanHouseEnergyDataSumByDay(MeterNo,MeterType,DAYear,DAMonth,DADay,SumQty,Unit)
Select @MeterNo,@MeterType,@DAYear,@DAMonth,@DADay,@Qty,@Unit;
End

if (@@error <> 0)
begin
goto LABROLLBACK
end

If @RowCnt_Hour > 0
Begin
Update TanHouseEnergyDataSumByHour Set SumQty = SumQty + @Qty
Where MeterNo = @MeterNo And MeterType = @MeterType And DADay = IsNull(@DAYear,‘‘) + IsNull(@DAMonth,‘‘) + IsNull(@DADay,‘‘)
And DAHour = @DAHour ;
End
Else
Begin
Insert TanHouseEnergyDataSumByHour(MeterNo,MeterType,DADay,DAHour,SumQty,Unit)
Select @MeterNo,@MeterType,IsNull(@DAYear,‘‘) + IsNull(@DAMonth,‘‘) + IsNull(@DADay,‘‘),@DAHour,@Qty,@Unit;
End

----------

--发生错误回滚事务
if (@@error <> 0)
begin
goto LABROLLBACK
end

--提交事务
LABCOMMIT:
if (@@error = 0)
begin
COMMIT TRANSACTION
return(0)
end
--以下回滚事务
LABROLLBACK:
begin
ROLLBACK TRANSACTION sp_Datacomputer_TRANS
return @@error
end

--进行数据计算的事务结束------------------------------------------------------------------------------------------------------
--end --If End

--else
--begin
-- Insert TanHouseDatasByRealTime(DANo,DATime,LogTime,MeterType,MeterNo,Qty)
-- Select @DANo,@DATime,@LogTime,@MeterType,@MeterNo,@Qty;
--end --Else End

end --存储过程结束End

带存储过程的事务模板:

ALTER procedure [dbo].[sp_TRANSACTION_Templater]
@tblname nvarchar(776),
@flagc varchar(10)=null,
@indname sysname=null
as

--带事务存储过程模板

begin
--开始事务
BEGIN TRANSACTION
SAVE TRANSACTION sp_Datacomputer_TRANS

--事务内容

----------

--发生错误回滚事务
if (@@error <> 0)
begin
goto LABROLLBACK
end

--提交事务
LABCOMMIT:
if (@@error = 0)
begin
COMMIT TRANSACTION
return(0)
end
--以下回滚事务
LABROLLBACK:
begin
ROLLBACK TRANSACTION sp_Datacomputer_TRANS
return @@error
end

end

循环计算”时、日、月、年“数据的方法思路,布布扣,bubuko.com

时间: 2024-10-10 14:09:25

循环计算”时、日、月、年“数据的方法思路的相关文章

MySql按周/月/日分组统计数据的方法

知识关键词:DATE_FORMAT select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks; select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days; select DATE_FORMAT(create_time,'%Y%m') month

基于Flink秒级计算时CPU监控图表数据中断问题

基于Flink进行秒级计算时,发现监控图表中CPU有数据中断现象,通过一段时间的跟踪定位,该问题目前已得到有效解决,以下是解决思路: 一.问题现象 以SQL02为例,发现本来10秒一个点的数据,有时会出现断点现象,会少1-2个点甚至更多: 二.问题定位 针对该问题,根据数据处理链路,制定了数据输出跟踪示意图,如下所示: 通过输出的实际数据发现: 1.监控Agent的数据已经正确上报Kafka 2.从Kafka中可以正确取到监控Agent上报的数据 3.从计算完毕的Kafka中取不到丢失点的数据

浅谈webform开发时前台请求后台数据的方法

说到前台请求后台数据,我们一般都是用到AJAX(异步JavaScript和XML) .AJAX 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,我们可以对网页的某部分进行更新.在这里,主要浅谈一下在.net的webform开发时,前台请求后台的两种方式. 1.使用AjaxPro2.dll  (1)AjaxPro2.dll文件可以去网上下载,下载后引用到项目中. (2)引用到项目之后,在web.config里面的<system.web

通过EF操作Sqlite时遇到的问题及解决方法

1.使用Guid作为字段类型时,能存,能查,但是作为查询条件时查询不到数据 解决方法:连接字符串加上;binaryguid=False 原文地址:https://www.cnblogs.com/amber-L/p/9584096.html

输入年 月 日 ,计算时该年的第几天

#include "stdio.h" void main() { /*year 年 month 月 month1 备份月数 day 日 day1 备份总体天数,day用于接收当月的天数,d1备份当月的天数. s 计算当月之前的月的总天数 sum 天数总和 */ int year,month,month1,day,day1,d1,sum=0,s=0; printf("请输入年份:"); scanf("%d",&year); fflush(s

计算两日期间2月29日总数的Java程序

事先声明,本人仅仅是个计算机领域的新手,不久前开始学习Java.后来我接到了一份关于计算两日期间2月29日总数的编程作业,仓促之中我便写下了这个程序.由于之前可以说毫无编程经验,Java也仅仅是只学了一小部分.所以目前该程序尚未解决输入问题. 我解决这个问题的思路是: 编写一个方法(leapYear)用于判断某一年份是否为闰年: 编写另一个方法(dateExist)用于判断某一时期是否真实存在,在这方法中会引用到上个方法: 最后编写main方法,引用方法(dateExist)分别判断起始日期和终

MySql 按周/月/日统计数据的方法

知识关键词:DATE_FORMAT select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks; select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days; select DATE_FORMAT(create_time,'%Y%m') month

7月21日 企业大数据平台仓库架构建设思路直播视频

7月21日 企业大数据平台仓库架构建设思路--李金波(阿里云高级技术专家) 采访:我们该如何做好一个数据仓库? 演讲视频:https://yq.aliyun.com/edu/lesson/119 演讲整理文章:https://yq.aliyun.com/articles/57901 PDF下载:点此进入 现场问答整理:https://yq.aliyun.com/ask/36706/ 更多技术峰会视频资料详见:https://yq.aliyun.com/articles/57826 阅读原文请点击

mysql按月,按日分组统计数据

1 select DATE_FORMAT(createtime,'%Y-%m'),count(*) from test where user =8 group by DATE_FORMAT(createtime,'%Y-%m');//按月统计数据 2 select DATE_FORMAT(createtime,'%Y-%m-%d'),count(*) from test where user =8 group by DATE_FORMAT(createtime,'%Y-%m-%d');//按天统