Re: calculating 28 days from db date posted col
| From: | Adam Royle | Date: | Wed, 07 Aug 2002 11:39:34 +0000 |
| Subject: | Re: calculating 28 days from db date posted col | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-21044@lists.php.net to get a copy of this message | ||
Hi Dave,
I wanted to do this a little while ago with a finance program I developed. What I wanted it to do was grab the amount of expenditure each month (or week). So I wrote the following sql.
SELECT YEAR(purchaseDate) AS year, MONTH(purchaseDate) AS month, SUM(itemPrice) AS total
FROM tblCashflow
WHERE Type = 'Expenditure'
GROUP BY year, month
ORDER BY year, month
This works pretty well, but it only collects info for the months that have an expenditure. What happens if there is a month where I don't spend any money (i wish)? It won't show $0 - it just won't appear. Obviously this is a big flaw. At the time I didn't know a way around this, so I wrote a heap of php code to analyse the data, and print it out correctly. I got it fine in the end (with more features, etc).
I asked a guy at work about this, and how he would have done something similar. He muttered words about UNIONs in sql, and said it is possible to do it just using SQL.
Now I still don't know how to do it in SQL, but someone on this list might. If anyone knows a way, please post it (or send to me). Dave, if you want the PHP code I used, just ask - it's only a cut and paste away!
Adam