简体   繁体   中英

Counting column sum in sql server table

I have try to record numbers on to a db, but I can't insert column sum. Example:

Records:

check1      check2      check3       sum_number        roll_sum
  500         510         500           3.3               3.3(should be)
  490         500         505          -1.6               1.7 (should be)

But my roll sum's always is NULL .

My PHP script is

($sum_number + (SELECT SUM(roll_sum) FROM table_name))

But, if I manually put first number to roll_sum than script counting another rows fine. How do I make script work fine?

尝试这个:

SELECT SUM(Case ROLL_SUM WHEN NULL THEN SUM_NUMBER ELSE ROLL_SUM END AS ROLL_SUM) FROM table_name     

use over clause

select * ,
SUM(sum_number) OVER (ORDER BY fieldName ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )roll_sum
from table_name 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM