mysql - Sql Server default value average of x rows -


i have following trigger running on mysql:

create definer=`root`@`%` trigger `before_insert` before insert on `table` each row set new.avg_column1 = (select avg(column1) (select column1 table order datetimecol desc limit 20) column1_a),  new.avg_column2 = (select avg(column2) (select column2 table order datetimecol desc limit 20) column2_a),  new.avg_column3 = (select avg(column3) (select column3 table order datetimecol desc limit 20) column3_a) 

basically goal here set automatic, default value in avg_columnx column, based on last 20 entries in columnx, whenever new row inserted. working fine in mysql using mentioned trigger.

i in process of migrating project sql server express ms, , i'm trying same there. have pointers how accomplish this? using triggers, computed columns, etc?

thanks input!

the logic different in sql server because using inserted rather new. basically:

update t     set avg_row1 = tt.avg1,          avg_row2 = tt.avg2,          avg_row3 = tt.avg3     table t join          inserted          on i.pk = t.pk outer apply          (select avg(row1) avg1, avg(row2) avg2, avg(row3) avg3           (select top 20 t.*                 table t                 order datetimerow desc                ) t          ) tt; 

you need identifier(s) in row match table inserted. pk stands for.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -