Shahadat Atom Shahadat Atom - 13 days ago 4
MySQL Question

how to sum after group by return in mysql?

I have a table like

ID amount item date
1 10 sr 21/11/2016
2 10 sr 22/11/2016
2 10 sr 22/11/2016
2 10 sr 22/11/2016
3 30 sr 22/11/2016
3 30 sr 22/11/2016
3 30 sr 22/11/2016


i just want return data like

amount date
10 21/11/2016
40 22/11/2016


What is the best way to get it?

may be group by by date, and only i amount for one id will pick and will sum then.
Thank You in Advance.

Answer

First eliminate duplicates with DISTINCT then a simple GROUP BY

DEMO

 SELECT date, SUM(amount)
 FROM ( SELECT DISTINCT ID, amount, item, date
        FROM YourTable ) as T
 GROUP BY date

OUTPUT

enter image description here

Comments