Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Postgresql中,需要计算两行值column_d2=(column_c2*0.3)+(column_c3*0.7)?通过更新功能_Sql_Postgresql - Fatal编程技术网

在Postgresql中,需要计算两行值column_d2=(column_c2*0.3)+(column_c3*0.7)?通过更新功能

在Postgresql中,需要计算两行值column_d2=(column_c2*0.3)+(column_c3*0.7)?通过更新功能,sql,postgresql,Sql,Postgresql,我创建了一个类似于test的表,并在列中插入了值 create table test( Sno integer , Column_A text , Column_B numeric , Column_C numeric ) ; insert into test (sno, Co

我创建了一个类似于test的表,并在列中插入了值

create table test( Sno       integer 
                 , Column_A   text
                 , Column_B   numeric
                 , Column_C   numeric
                 ) ;
                 
           
insert into test (sno, Column_A, Column_B) 
     (values (1 ,'0 -1 Yrs',     0.963190184)     
           , (2 ,'1-4 Yrs',      0.992394232)     
           , (3 ,'5 - 9 Yrs',    0.994964922)     
           , (4 ,'10 - 14 Yrs',  0.998372661)     
           , (5 ,'15 - 19 Yrs',  0.994485603)     
           , (6 ,'20- 24 Yrs',   0.992903887)     
           , (7 ,'25 - 29 Yrs',  0.994008987)     
           , (8 ,'30 - 34 Yrs',  0.994041445)     
           , (9 ,'35 - 39 Yrs',  0.991283828)     
           , (10,'40 - 44 Yrs',  0.987141228) 
     ); 


select * from test; 
  
alter table test add column column_d bigint; ----adding a new column

update test
   set column_c = r_colc
  from (
        with recursive demo( rno, r_colb, r_colc) as 
             ( select 0, 1::numeric,  100000::numeric  -- prime the recursion 
               union all 
               select sno, column_b,  r_colb*r_colc
                from test 
                join demo  
                  on (sno = (select min(sno)          -- since I do trust autogenerated ids 
                               from test              -- to actually be in perfect sequence
                              where sno > rno))    
             )
        select rno, r_colb, round(r_colc) r_colc
          from demo 
        ) s
  where sno = rno; 
  

select * from test; 
现在我需要计算两行值column_d2=column_c2*0.3+column_c3*0.7?通过更新功能


这实际上是一个简单的更新,假设我的评论中的列含义是正确的。使用LEAD window函数从下一行获取列的值。然后更新是一个简单的乘法:

update test t  
   set column_d = (cr*.3 + crn*.7)::bigint
  from (select sno, column_c cr, lead(column_c) over(order by sno) crn from test) s
 where t.sno = s.sno 
   and t.column_a not like '0%';

注:cr是当前行的第c列,crn是下一行的当前roe。但这显然回避了一个问题:如果没有下一行,你会为新列做什么。

我附上了表格详细信息图像格式标题与底部文本不匹配,查询与两者都不匹配看不到0.3或0.7请澄清你的问题hi@Turo,我需要将列值与默认值0.3和0.7相乘,然后将相乘的值相加,然后在另一列中打印结果。我已附上表格详细信息供您参考。您好@Turo,我编辑了标题我仍然不明白,但我要说的是,将其作为一个存储过程并在recordsHi@Belayer上循环,首先非常感谢您在本论坛中的大力支持。我真的很抱歉这么做,我以后不会这么做了。我将解释,每一列都有单独的计算。例如,第一列第2行=0.3*列第2行+0.7*列第3行-列第0-1年,下一列第3行=4/7*列第3行+列第4行-列第1-4年,下一列第4行=5/2*列第4行+列第5行-列第5行-列第5行-列第5行-列第5-9年至35-39年,最后一列第11行=列第8行/另一个字段值-列第80年以上是Yrs.Hi@Belayer,我已经根据您的更新查询完成了任务。现在,我已经修改了表列“如何将我的sql查询粘贴在此论坛中更新寿命预期寿命t set no_of_person_years_lived=startifer*0.3+startifer*0.7::bigint from select id,startife startifer,leadstartife overorder by id startifer from life_expection_vellore s其中t.id=s.id和t.age_range='0-1岁';