Skip to content
Advertisement

Need guidance on MySQL Query

I am having a table with values like this below one.

Col1 Col2 Col3 Col4 col5
1 John ABC 10 20
1 John AED 52 15
1 John ABC 12 10
1 John AED 20 5
1 John ABC 10 5
2 Mark DDD 42 8
2 Mark BBB 10 5
3 Ben EEE 8 2
3 Ben FFF 1 1
3 Ben EEE 2 3

I want to group them in the following manner

col1 col2 col3 col4 col5
1 John ABC 32 35
1 John AED 72 20
2 Mark DDD 42 8
2 Mark BBB 10 5
3 Ben EEE 10 5
3 Ben FFF 1 1

I used the

select col1, col2, col3, SUM(col4), SUM(col5)
from table
where [some condition]
group by col3

I am getting errors. Can somebody help with this?

Thanks,

Advertisement

Answer

group by close must have all selected attribues

group by col1, col2, col3, SUM(col4), SUM(col5)

The query become :

select col1, col2, col3, SUM(col4), SUM(col5)
from table
where [some condition]
group by col1, col2, col3
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement