postgresql - SQL Multiple update record from SELECT WHERE -
i need help.
i want totally quantity of material same id in bill.
like this
bill no. 001 jellopy x1ea jellopy x1ea jellopy x1ea zargon x1ea
result should be
bill no. 001 jellopy x3ea zargon x1ea
so,i want this
1.select+sum 2.update 3.delete duplicate
here old (dbill) table
dbill
id mat qty 01 a1 1 01 a1 1 01 a1 1 01 a2 1 id mat qty [01 a1 1] < same mat&id = sum [01 a1 1] < same mat&id = sum [01 a1 1] < same mat&id = sum 01 a2 1
and sqlfiddle test http://sqlfiddle.com/#!15/b30a3c
1 sum quantity same material
select mat,id,sum(qty) result dbill group id,mat
[2] got result this
id mat qty 01 a1 3 01 a2 1
[3] so... update this
update dbill set qty = result dbill t inner join (select mat,id,sum(qty) result dbill group id,mat) s on s.id = t.id , s.mat = t.mat
the problem when [3] update here result
id mat qty 01 a1 3 01 a1 3 01 a1 3 01 a2 3
because correct result should be
id mat qty 01 a1 3 01 a1 3 01 a1 3 01 a2 1 <
thank understand , answer ...
for me query seems ok replace update dbill
update t
update t <---- set qty = result dbill t inner join (select mat,id,sum(qty) result dbill group id,mat) s on s.id = t.id , s.mat = t.mat
Comments
Post a Comment