postgresql - SQL update with cursors/loops -
i have 3 sql tables follows:
- table #1 has column recipeid
- table #2 has column
- table #3 has columns recipeid, alimentid, quantity
in recipes, want replace occurrences of aliment aliment b.
example:
recipeid | alimentid | quantity ----------------------------------- recipe | aliment | 10. recipe | aliment b | 5. recipe b | aliment c | 2. recipe b | aliment b | 5. recipe c | aliment | 9.
replacing aliment aliment b.
expected output:
recipeid | alimentid | quantity ----------------------------------- recipe | aliment b | 15. recipe b | aliment c | 2. recipe b | aliment b | 5. recipe c | aliment b | 9.
another accepted output keep records quantity equal 0.
how in sql without cursor/loops ? there way update-set-select-join ? here, afraid need more set...
(i using postgresql.)
if understand correctly need
select recipeid,replace(alimentid,'aliment a','aliment b') alimentid, sum(quantity) quantity #table3 group recipeid,replace(alimentid,'aliment a','aliment b') alimentid
Comments
Post a Comment