SQL根據平均值等比例修改原始值

2021-08-10 05:12:31 字數 2569 閱讀 1642

由於業務需求資料顯示的年度報表數值時每日採集的整點資料的平均值,然而現場人員給我們反饋回來直接就是平均值,但是資料庫中原始儲存的資料是每日每個時間段的整點數值。

sws_st_spb_p 原始資料每天都有數個整點資料:

sws_st_spb_p_copy 這是我們修改後每日的平均值每天只有一條資料:

那麼我所要解決的問題是將sws_st_spb_p 原始資料中需要修改的資料根據修改過後的sws_st_spb_p_copy 中的資料等比例的縮放。

-- 刪除所要修改的原始資料

delete sc from sws_st_spb_p sc

inner

join sws_st_spb_p_copy s

on s.stcd = sc.stcd

and convert(varchar(10), s.tm, 126) = convert(varchar(10), sc.tm, 126)

and s.mpcd = sc.mpcd

where s.tm >='2017-09-01 00:00:00'

and s.tm < '2017-10-01 00:00:00'

and s.mpcd = sc.mpcd

-- 增加的字段

alter

table sws_st_spb_p_copy add r1sum decimal,r2sum decimal,r3sum decimal,rcount decimal ;

select * into sws_st_spb_p_copy1 from sws_st_spb_p;

-- 建立臨時表 aaa

select t.mpcd, sum(t.r1) as r1sum,sum(t.r2) as r2sum,sum(t.r3) as r3sum,count(*) rcount, convert(varchar(10), t.tm, 126) as ttime

into aaa from (

select s.mpcd,s.r1,s.r2,s.r3,sc.r1 as c1,sc.r2 as c2,sc.r3 as c3, s.tm as tm from sws_st_spb_p_copy1 s

left

join sws_st_spb_p_copy sc on s.stcd = sc.stcd

and convert(varchar(10), s.tm, 126) = convert(varchar(10), sc.tm, 126)

and s.mpcd = sc.mpcd

where s.tm >='2017-09-01 00:00:00'

and s.tm < '2017-10-01 00:00:00'

and s.mpcd = sc.mpcd

) t

group

by t.mpcd ,convert(varchar(10), t.tm, 126)

select a.*,s.r1,s.r2 into result from aaa a

left

join sws_st_spb_p_copy s

on a.mpcd = s.mpcd and a.ttime = s.tm

where s.tm >='2017-09-01 00:00:00'

and s.tm < '2017-10-01 00:00:00'

and s.mpcd = a.mpcd

select t.stcd, t.mpcd,t.r1*(t.c1*rcount)/t.r1sum as r1,t.r2*(t.c2*rcount)/t.r2sum as r2,t.tm

into resultspb from (

select s.stcd, s.mpcd,s.r1,s.r2,s.r3, s.tm as tm,sc.r1 as c1,sc.r2 as c2,sc.r1sum as r1sum,sc.r2sum as r2sum,sc.r3sum as r3sum,sc.rcount as rcount from sws_st_spb_p_copy1 s

left

join result sc on

convert(varchar(10), s.tm, 126) = convert(varchar(10), sc.ttime, 126)

and s.mpcd = sc.mpcd

where s.tm >='2017-09-01 00:00:00'

and s.tm <='2017-09-30 23:59:00'

and s.mpcd = sc.mpcd

)t ;

php 平均值,PHP 根據日期值計算平均值

我有乙個帶有日期和評分的陣列.摘錄如下 array 111 0 array 2 date string 19 2018 03 03 17 15 42 rating int 3 1 array 2 date string 19 2018 02 24 09 56 03 rating int 1 2 ar...

樹上平均值

給出一棵樹,求出乙個結點的集合,該集合滿足條件 1 根結點不在集合中 2 任何兩個節點只有乙個公共祖先 3 每個節點要兩個值,w 根結點到這個結點的路徑上所有邊的權值和 d 深度 對每個集合求w之和 d之和的最大值。include include include includeusing names...

平均值編碼

針對高基數定性特徵 類別特徵 的資料預處理 一般情況下,針對定性特徵,我們只需要使用sklearn的onehotencoder或labelencoder進行編碼 onehotencoder 則能通過啞編碼,製作出乙個m n的稀疏矩陣 labelencoder 能夠接收不規則的特徵列,並將其轉化為從l...