Write a query to get the sum of similar ids to display on each record

Viewed 197

I want to calculate the sum of the same ids and display in each record. expected Sample is given below

Expectd result

2 Answers

Use analytics SUM():

SELECT
      Code, SUM(Value) OVER (PARTITION BY Code) as Value
  FROM mytable;

Summing over the window should allow you to maintain the overall sum for each record. ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ensures that all values are taken into consideration

SELECT
   Code,
   SUM(Value) OVER (PARTITION BY Code ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM
  mytable;
Related