2

From MySQL table I have the list amount based on the dates. I need to get the sum of amount for each date:

ex:

id  type    date    amount  
1   1   2015-01-01  100
2   1   2015-01-01  150
3   1   2015-01-02  10
4   1   2015-01-03  250

Here 2015-01-01 appears more than once.

so i need the result like

date    amount  
2015-01-01  200
2015-01-02  10
2015-01-03  250

My Query getting between this week start and end

 SELECT * from mytable WHERE YEARWEEK(`date`) = YEARWEEK(CURRENT_DATE) AND `type` = 1 ORDER BY `date` ASC 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Subha
  • 741
  • 2
  • 9
  • 23

3 Answers3

3

You need a group by clause:

SELECT   `date`, SUM(amount)
FROM     mytable
GROUP BY `date`
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

I think the result should be

date    amount  
2015-01-01  250
2015-01-02  10
2015-01-03  250

you can use this mysql query to get that result:

Select date, sum(amount) as amount 
from mytable 
GROUP BY date
ORDER BY date asc

dont forget to add "ORDER BY" clause if you want the result in good order

setyo
  • 47
  • 12
1

Use GROUP BY

Use AS clause to change the column Name

SELECT   `date`, SUM(amount) AS amount 
FROM     mytable
GROUP BY `date` 
Surya
  • 454
  • 6
  • 15