Agregate function with multiple condition

Advertisements

how to achive in mysql sth like this:

select 
sum(B.someField) where B.someIntFild = 1
sum(B.someField) where B.someintFild = 2
from tableA A
inner join tableB B on A.id = B.id
where A.id = 'someId'

which are two sums on one group but first sum is for items with someIntFild = 1 and second someIntFild = 2 ?

>Solution :

Use conditional aggregation, eg

sum(case when B.someIntFild = 1 then B.somefield else 0 end) as Col1,
sum(case when B.someIntFild = 2 then B.somefield else 0 end) as Col2

etc

Leave a ReplyCancel reply