Reputation: 626
I have a mysql table like this
id criteria1 criteria2 amount
1 'a' 'b' 100
2 'a' 'c' 20
3 'd' 'b' 30
I need to write a group by query in such a way that my final output should be
criteriaMatch sum(amount)
criteria2 = b 130
criteria1 = a 120
Data with id 1 overlaps in both condition.
How do i achieve this?
I tried using mysql case. But in that case when the first criteria matches, the data skips the second condition and I dont get the intended result.
Upvotes: 0
Views: 32
Reputation: 21757
You could create 2 separate queries, the first grouping only by criteria 1 and the second only by criteria 2, along with proper filters in each one. So, something like this:
select criteria1 'Criteria',sum(amt)
from yourtable
where (criteria1 = 'a')
group by criteria1
union
select criteria2 'Criteria',sum(amt)
from yourtable
where (criteria2 = 'b')
group by criteria2
Upvotes: 3