PDA

View Full Version : Resolved Query For Summing Values For Each Day


nanoop13
08-04-2009, 01:57 AM
Hi friends, I wanted to get the total amount earned by my customer each day and display it.
He sells his product 10-20 times a day.Now in the dashboard He will select the dates range and will view the amount earned each day.
Table1 have columns: date,SP
the content in Table1 is somewhat like:
2009-08-03 24
2009-08-03 30
2009-08-03 09
2009-08-02 90
2009-08-02 75
2009-08-01 32
2009-08-01 30
2009-08-01 20
I was in search of a query which would return the result
2009-08-03=>63
2009-08-02=>165
2009-08-01=>82
means in that particular day he made a total sales of the corresponding amount.
Please tell me the any query which would give the desired results.I have been messing round the whole night experimenting with new syntax but failed to get the result.
I will be highly obliged for any help.

Old Pedant
08-04-2009, 02:03 AM
SELECT `date`, SUM(SP) AS totalsales
FROM sometable
WHERE `date` BETWEEN '2009-08-01' AND '2009-08-07'
GROUP BY `date`
ORDER BY `date`

You need to use `date` because DATE is a builtin function in MySQL and could cause confusion. *IF* the `date` field in that table contains *ONLY* dates (not dates and times) then the above should work. If the `date` field contains both date *and* times then a minor mod:

SELECT `date`, SUM(SP) AS totalsales
FROM sometable
WHERE DATE(`date`) BETWEEN '2009-08-01' AND '2009-08-07'
GROUP BY `date`
ORDER BY `date`

And now you know what the builtin DATE() function does: It extracts the date-only from a datetime value.

Naturally you would use your server-side language (PHP, ASP, whatever) to generate the two date strings ('2009-08-01' and '2009-08-07') and plunk them into the query.

nanoop13
08-04-2009, 03:43 AM
Thank you bro.....You rock!!