| Author |
Topic |
|
bpsintl
Posting Yak Master
132 Posts |
Posted - 2009-03-26 : 09:16:21
|
| I need some ideas on the best way to do this:I am trying to work out profit and loss results from Jan to Dec for a given year.I have 5 income categories (1 to 5), and my income table is as follows:income_iddate_receivedinvoiceid (foreign key linking to an invoice table)amounton_bank_statementAt the moment I am looping through on the web-page with sql likeselect sum(income.amount) as totfrom invoices inner join income on invoices.id = income.invoiceidwhere income.date_received between '2009/01/01' and '2009/01/31' and invoices.type=1That would obviously calculate the total income in Jan 2009 for the income category 1 (type=1)I am looping through the page opening and closing record sets 5 times per month, so thats 5 x 12 = 60 times!There must be a better way of doing this? |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-26 : 09:25:02
|
| [code]select sum(income.amount) as tot,invoices.type,month(income.date_received)from invoices inner join income on invoices.id = income.invoiceidwhere income.date_received between '20090101' and '20091231' and invoices.type in (1,2,3,4,5)group by invoices.type,month(income.date_received)[/code] |
 |
|
|
bpsintl
Posting Yak Master
132 Posts |
Posted - 2009-03-26 : 09:35:47
|
| You are the man! (or woman)... Many thanks |
 |
|
|
bpsintl
Posting Yak Master
132 Posts |
Posted - 2009-03-26 : 10:01:55
|
| Although that query does the job brilliantly, how can I expand on it to include the column sum totals?ie the sum of everyting in Jan, sum of everything in feb etc so that I can have a "Totals" column in the table at the bottom.Does that make sense? |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-26 : 10:48:58
|
try this,select sum(income.amount) as tot,invoices.type,month(income.date_received)from invoices inner join income on invoices.id = income.invoiceidwhere income.date_received between '20090101' and '20091231' and invoices.type in (1,2,3,4,5)group by invoices.type,month(income.date_received)With Rollup |
 |
|
|
bpsintl
Posting Yak Master
132 Posts |
Posted - 2009-03-26 : 12:25:20
|
| What is Rollup? |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-26 : 12:35:52
|
| try looking in BOL. That explains it well. |
 |
|
|
bpsintl
Posting Yak Master
132 Posts |
Posted - 2009-03-26 : 12:48:03
|
| Great, thanks for that! |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-26 : 12:59:39
|
| np |
 |
|
|
bpsintl
Posting Yak Master
132 Posts |
Posted - 2009-03-26 : 13:31:43
|
| One more question! How can I workout the percentage for each month against the total? |
 |
|
|
bpsintl
Posting Yak Master
132 Posts |
Posted - 2009-03-26 : 14:32:59
|
| Ignore that last post please, I've done it in code on the page |
 |
|
|
|