Not long ago I was given a task to implement report. The problem that i am facing is the following. I have a table:
name value type
------------------------
100 Kasa | 50000 | B
100 Kasa | 30000 | A
80 Kasa | 500 | A
80 Kasa | 50 | B
200 Hello | 3000 | A
645 Bye | 1234 | B
What I want to achieve is group by name, sum values if type is same and if type is different then write it into another cell. I have only 2 types.
I want to get result as
name valueB valueA
--------------------------
100 Kasa | 50000 | 30000
80 Kasa | 0 | 550
200 Hello | 0 | 3000
645 Bye | 1234 | 0
For now, I am grouping them in php, which makes a mess in code, and now i am hoping that someone can help me to do it with mysql
I think your expected output (result) is wrong
It should be:
You can use
SUMfunction for thatWith
CASEstatement:Or with
IFstatement:See this SQLFiddle
If your expected output is like this (
NULLinstead of0)See this SQLFiddle