0

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,

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • Your have to create a [mre], so `SELECT ... where [some condition] ...` is not OK. when the condition is not important for your question, you could easily remove the `where [some condition]` – Luuk Mar 18 '22 at 10:53
  • "I am getting errors " Please copy/paste the complete error message (as text, not as an image) – Luuk Mar 18 '22 at 10:54
  • also, the error the query is returning is the basic for debugging. You may just have a syntax error in the query you are running but not showing – Lelio Faieta Mar 18 '22 at 10:55
  • 2
    You should be grouping by all non aggregating columns.. – P.Salmon Mar 18 '22 at 11:00
  • 1
    The error "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_1808046991.table1.col1' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by" (see: [DBFIDDLE](https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=975783efbd4020a2aab0707d46c1e1d8)) is answered multiple times on stackoverflow. a correct answer might be: https://stackoverflow.com/a/41887524/724039 (not https://stackoverflow.com/a/41887627/724039 !) – Luuk Mar 18 '22 at 11:01
  • I.e. try `GROUP BY col1, col2, col3`. – jarlh Mar 18 '22 at 11:33

1 Answers1

1

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
Bessem Manita
  • 220
  • 1
  • 6