I have following table.
---+---------------+-------------+--------+
id | department_id | created | amount |
---+---------------+-------------+--------+
1 | 0 | 2016-05-05 | 500 |
2 | 2 | 2016-05-05 | 100 |
3 | 1 | 2016-05-05 | 160 |
4 | 2 | 2016-05-05 | 260 |
5 | 1 | 2016-05-04 | 100 |
6 | 0 | 2016-05-04 | 150 |
7 | 1 | 2016-05-04 | 160 |
8 | 2 | 2016-05-04 | 160 |
---+---------------+-------------+--------+
I want to generate report to show how much collection is there on particular day in particular department & need to show like below table:
Date | 0 | 1 | 2 |
2016-05-05 | 500 | 160 | 360 |
2016-05-04 | 150 | 260 | 160 |
I am using cakephp. If I use GROUP BY created it works and if I use GROUP BY department_id it works. But I need both GROUP BY to generate the required report.
I also tried generating the from to date array using this. and then tried running GROUP BY created on each date using following CakePHP query:
//$ar = date array
foreach ($ar as $k) {
$this->Transaction->find('all',array('conditions'=>array('Transaction.created'=>$k),'fields'=>array('SUM(Transaction.amount) AS s','Transaction.department_id'),'group'=>array('Transaction.department_id')));
}
But it seems like slow solution to me if date range is long. Is there any best way to do it in single query.
Please help me sorting this problem.