Need a sum on my current column
If I understand this correctly, what you want to do is successively subtract the value of SUM(v1 + v2 + v3) OVER (PARTITION BY year) from m for each row where st is 1, until m is exhausted; once the result drops to zero or below, it should show just 0.

Since the subtracted value is the same, you could just enumerate all st=1 rows and subtract sum * row_number for each row, like this:

WITH calculated AS (
  SELECT
    mytable.*,
    m - SUM(v1 + v2 + v3) OVER (PARTITION BY yr)
      * ROW_NUMBER()      OVER (PARTITION BY yr, st ORDER BY id ASC) AS res
  FROM
    mytable
)
SELECT
  id,
  yr,
  m,
  v1,
  v2,
  v3,
  st,
  CASE WHEN st = 1 AND res &gt; 0 THEN res ELSE 0 END AS my
FROM
  calculated
;


The above assumes that m is the same across all rows of a partition.