当前位置: 代码迷 >> Sql Server >> sql 多表关联计算的有关问题
  详细解决方案

sql 多表关联计算的有关问题

热度:94   发布时间:2016-04-27 11:51:20.0
sql 多表关联计算的问题
日降雨量表 数据是人录入的 记录 一天24小时的降雨量 并有字段 hj 为24小时降雨量的和

现有 年降雨量表 有 一年12个月的降雨量字段 和年降雨量总计字段 现有需求 年降雨的数据不用录入 直接根据日降雨量来计算
就是要根据日降雨量表中的数据来计算得到 一个月的降雨量 和一年降雨量

新增一天的数据 就对年降雨量表中对应月和年的记录更新

现在的问题 用什么方式来实现 存储过程? 触发器 ?

日降雨量

id 记录时间 1时,2时,3时......24时 hj
 
年降雨量

id 年份 1月份 2月份 3月份...... 12月份 hj

------解决方案--------------------
SQL code
--存储过程简单实现:--id 记录时间 1时,2时,3时......24时 hjcreate table #byday(id int identity,dt datetime,hour1 float,hour2 float,hour3 float,hour4 float    ,hour5 float,hour6 float,hour7 float,hour8 float,hour9 float,hour10 float    ,hour11 float,hour12 float,hour13 float,hour14 float,hour15 float,hour16 float,hour17 float,hour18 float,hour19 float    ,hour20 float,hour21 float,hour22 float,hour23 float,hour24 float) --年降雨量--id 年份 1月份 2月份 3月份...... 12月份 hjcreate table #byyear(id int identity,year int,month1 float,month2 float,month3 float,month4 float,month5 float,month6 float    ,month7 float,month8 float,month9 float,month10 float,month11 float,month12 float)select * from #bydayselect * from #byyeargocreate procedure usp_sync_year(    @dt datetime    ,@hour1 float,@hour2 float,@hour3 float,@hour4 float    ,@hour5 float,@hour6 float,@hour7 float,@hour8 float,@hour9 float,@hour10 float    ,@hour11 float,@hour12 float,@hour13 float,@hour14 float,@hour15 float,@hour16 float,@hour17 float,@hour18 float,@hour19 float    ,@hour20 float,@hour21 float,@hour22 float,@hour23 float,@hour24 float)asbegin    insert into #byday(dt,hour1,hour2,hour3,hour4,hour5,hour6,hour7,hour8,hour9,hour10,hour11,hour12        ,hour13,hour14,hour15,hour16,hour17,hour18,hour19,hour20,hour21,hour22,hour23,hour24)    select @dt,@hour1,@hour2,@hour3,@hour4,@hour5,@hour6,@hour7,@hour8,@hour9,@hour10,@hour11,@hour12        ,@hour13,@hour14,@hour15,@hour16,@hour17,@hour18,@hour19,@hour20,@hour21,@hour22,@hour23,@hour24        declare @all_count float    set @[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]        [email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]        if @all_count =0     begin        return    end    declare @year int,@month int    select @year=YEAR(@dt),@month=MONTH(@dt)    if not exists(select id from #byyear where [year][email protected])    begin        insert into #byyear(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)        select @year,0,0,0,0,0,0,0,0,0,0,0,0    end    declare @sql varchar(1000)    set @sql='update #byyear set month'+cast(@month as varchar(5))+'=month'+cast(@month as varchar(5))+'+'+cast(@all_count as varchar(5))+' where year='+cast(@year as varchar(5))        exec sp_sqlexec @sql        returnend
  相关解决方案