选择SUM然后循环更新

时间:2018-01-10 16:30:30

标签: sql oracle

我想要列FLAN01+FLAN02+FLAN03+FLAN04列的总和,并在每个flnumb的flawtd中加上总和。所以我需要你的帮助,谢谢

FLAID   FLCTRY  FLFY    FLLT    FLAPYC      FLAN01  FLAN02  FLAN03  FLAN04  FLAWTD  FLNUMB
2749023 20      17      AA      -2832227    0       0       0       0       0       1
2524    20      17      AA      -164999     0       0       0       0       0       2
2749023 20      17      AA      -2460920    0       0       0       0       0       3
2749023 20      17      AA      -2756040    0       0       0       0       0       4
2524    20      17      AA      -197730     0       0       0       0       0       5
2749277 20      17      AA      -133875     0       0       0       0       0       6
2749091 20      17      AA      -957654     -8619   -8619   -8620   -8619   -94812  7
2749091 20      17      AA      -957654     -8619   -8619   -8620   -8619   -94812  8
2749091 20      17      AA      -957654     -8619   -8619   -8620   -8619   -94812  9
2749091 20      17      AA      -957654     -8619   -8619   -8620   -8619   -94812  10
2749091 20      17      AA      -957654     -8619   -8619   -8620   -8619   -94812  11
2749091 20      17      AA      -921543     -9314   -9314   -9314   -9314   -102453 12
2749091 20      17      AA      -957654     -8619   -8619   -8620   -8619   -94812  13
2749091 20      17      AA      -921543     -9314   -9314   -9314   -9314   -102453 14
2749091 20      17      AA      -921543     -9314   -9314   -9314   -9314   -102453 15

2 个答案:

答案 0 :(得分:0)

尝试使用这样的UPDATE操作:

UPDATE <table_name>
SET    FLAWTD = FLAN01+FLAN02+FLAN03+FLAN04

数据库SQL参考 https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10007.htm

答案 1 :(得分:0)

您必须使用MySQL IFNULL function为每个非null的flnumb值添加值,确保它们不为空。

UPDATE <table_name>
SET FLAWTD=IFNULL(FLAN01,0)+IFNULL(FLAN02,0)+IFNULL(FLAN03,0)+IFNULL(FLAN04,0)
WHERE flnumb IS NOT NULL;