Sql 基于组更新表

Sql 基于组更新表,sql,sql-server,Sql,Sql Server,我有一张像这样的桌子 ID PCode baseline Period Actual Forecast --------------------------------------------- 1 P231 7 5 100 0 2 P231 7 6 120 0 3 P231 7 7

我有一张像这样的桌子

ID    PCode   baseline    Period   Actual   Forecast
---------------------------------------------
1      P231        7           5       100       0
2      P231        7           6       120       0
3      P231        7           7       125       0
4      P231        7           8       130       120
5      P232        7           9       135       0
6      P232        7           5       100       0
7      P232        7           6       120       0
8      P232        7           7       125       0
9      P232        7           8       130       60
10     P233        7           9       135       0
11     P233        7           5       100       0
12     P233        7           7       120       0
13     P233        7           8       125       70
14     P231        8           1       130       0
15     P231        8           2       135       0
16     P231        8           8       130       0
17     P231        8           3       135       60
18     P232        8           2       130       0
19     P232        8           3       135       0
20     P232        8           8       130       0
21     P232        8           4       135       0
22     P232        8           5       130       70
23     P233        8           3       135       50
ID    PCode   baseline    Period   Actual   Forecast
---------------------------------------------
1      P231        7           5       100       0
2      P231        7           6       120       0
3      P231        7           7       125       120
4      P231        7           8       130       0
5      P232        7           9       135       0
6      P232        7           5       100       0
7      P232        7           6       120       0
8      P232        7           7       125       60
9      P232        7           8       130       0
10     P233        7           9       135       0
11     P233        7           5       100       0
12     P233        7           7       120       70
13     P233        7           8       125       0
14     P231        8           1       130       0
15     P231        8           2       135       0
16     P231        8           8       130       60
17     P231        8           3       135       0
18     P232        8           2       130       0
19     P232        8           3       135       0
20     P232        8           8       130       70
21     P232        8           4       135       0
22     P232        8           5       130       0
23     P233        8           3       135       0
我在上表中有一个要求,即在基线和期间相同的情况下,将每个PCode的预测值移动到一个组中,然后将该基线组中该PCode的最大预测复制到该位置

上表是这样的

ID    PCode   baseline    Period   Actual   Forecast
---------------------------------------------
1      P231        7           5       100       0
2      P231        7           6       120       0
3      P231        7           7       125       0
4      P231        7           8       130       120
5      P232        7           9       135       0
6      P232        7           5       100       0
7      P232        7           6       120       0
8      P232        7           7       125       0
9      P232        7           8       130       60
10     P233        7           9       135       0
11     P233        7           5       100       0
12     P233        7           7       120       0
13     P233        7           8       125       70
14     P231        8           1       130       0
15     P231        8           2       135       0
16     P231        8           8       130       0
17     P231        8           3       135       60
18     P232        8           2       130       0
19     P232        8           3       135       0
20     P232        8           8       130       0
21     P232        8           4       135       0
22     P232        8           5       130       70
23     P233        8           3       135       50
ID    PCode   baseline    Period   Actual   Forecast
---------------------------------------------
1      P231        7           5       100       0
2      P231        7           6       120       0
3      P231        7           7       125       120
4      P231        7           8       130       0
5      P232        7           9       135       0
6      P232        7           5       100       0
7      P232        7           6       120       0
8      P232        7           7       125       60
9      P232        7           8       130       0
10     P233        7           9       135       0
11     P233        7           5       100       0
12     P233        7           7       120       70
13     P233        7           8       125       0
14     P231        8           1       130       0
15     P231        8           2       135       0
16     P231        8           8       130       60
17     P231        8           3       135       0
18     P232        8           2       130       0
19     P232        8           3       135       0
20     P232        8           8       130       70
21     P232        8           4       135       0
22     P232        8           5       130       0
23     P233        8           3       135       0
我想尝试使用游标在所有基线之间循环。但是行的数量非常大。因此,我认为光标不是一个好的选择

试试这个:

update t
    set Forecast=   case
                        when t.baseline=t.Period then forecast_per_PCode.max_forecast
                        else 0
                    end
from yourtable t
inner join
(
    select t2.PCode,max(t2.Forecast) as max_forecast
    from yourtable t2
    group by t2.PCode
)forecast_per_PCode on t.PCode=forecast_per_PCode.PCode
试试这个

with perimeter as (
select Pcode from #YOURTABLE where BaseLine=Period
),
MaxForcast as (
select f1.Pcode, isnull(Max(f1.Forecast), 0) MaxForecast from #YOURTABLE f1
where exists (select * from perimeter f2 where f1.Pcode=f2.Pcode)
group by f1.Pcode
)
update f1
set f1.Forecast= case when f1.BaseLine=f1.Period then f2.MaxForecast else 0 end
from #YOURTABLE f1 inner join MaxForcast f2 on f1.Pcode=f2.Pcode
使用可更新的CTE:

with toupdate as (
      select t.*,
             max(forecast) over (partition by pcode) as group_forecast
      from t
     )
update toupdate
    set forecast = (case when baseline = period then group_forecast else 0 end)
    where forecast <> 0 or baseline = period;
但是,为什么不将预测放在组的所有行上呢


请注意,在这种情况下,窗口函数通常比连接快。另外,这只会更新需要更新的行。

是的,使用光标不好。顺便问一下,你的桌子名是什么?