STO
STO

Reputation: 133

Merging two (or more) "group by" like query result table

I have a table like this:

name area timestamp  
aa   10   timestamp  
aa   12   timestamp  
aa   22   timestamp  
bb   11   timestamp  
bb   11   timestamp  
cc   11   timestamp  

I can do the followings:

select name, sum(area) as sum1 from mytable where "condition1 based on timestamp" group by name;  
select name, sum(area) as sum2 from mytable where "condition2 based on timestamp" group by name;

But what I really need should look like this:

name sum1 sum2  
aa   444  555  
bb   666  777  
cc   111  222  

Any ideas what can it be done?

Upvotes: 0

Views: 26

Answers (1)

user330315
user330315

Reputation:

Use conditional aggregation:

select name, 
       sum(area) filter (where condition1) as sum1, 
       sum(area) filter (where condition2) as sum2
group by name;

Upvotes: 1

Related Questions