this is my table called “plan”
Plan No. | Weight |
---|---|
T190234 | 21 |
T190234 | 14 |
T190234 | 35 |
T190245 | 100 |
T190245 | 80 |
T190339 | 12 |
T190339 | 45 |
T190339 | 33 |
This should be my expected result: sum of column “totalwight”
Plan No. | Weight | totalwight |
---|---|---|
T190234 | 21 | 70 |
T190234 | 14 | 70 |
T190234 | 35 | 70 |
T190245 | 100 | 180 |
T190245 | 80 | 180 |
T190339 | 12 | 80 |
T190339 | 45 | 80 |
T190339 | 33 | 80 |
0
Use a windows function
to achieve this…
select
plan_no,
weight,
sum(weight) over (partition by plan_no) as total_weight
from plan
plan_no | weight | total_weight |
---|---|---|
T190234 | 21 | 70 |
T190234 | 14 | 70 |
T190234 | 35 | 70 |
T190245 | 100 | 180 |
T190245 | 80 | 180 |
T190339 | 12 | 90 |
T190339 | 45 | 90 |
T190339 | 33 | 90 |
fiddle