My resultant out is this using case when
in postgres
id name amount1 amount2 amount3
1 abc 10 null null
1 abc null 5 null
1 abc null null 40
I want my output as:
id name amount1 amount2 amount3
1 abc 10 5 40
2 aaa 90 20 10
2
Answers
You can do it using
group by
and the aggregate functionmax()
( usesum()
if you want tosum
your data by id ) :You can aggregate using
group by
andcoalesce
null
values to 0:I understand you have a
case when
and your input in the question is the result of that. Without knowing more about your tables, the best I can suggest is to replaceyourtable
in the query above with(<yourquery>) t
and of course, you need to replace<yourquery>
with your actual query.See this fiddle: http://sqlfiddle.com/#!15/e568d0/6
Test DB: