I have a table which looks like:
id | Item | Quantity | Amount | created
________________________________________________
1 | Monitor | 10 | 5000 | 2013-01-11
2 | Keyboard| 10 | 200 | 2013-02-19
3 | Monitor | 10 | 5000 | 2013-02-13
3 | Keybard | 10 | 200 | 2013-04-19
when I pass the query:
SELECT monthname( created ) AS
MONTH , sum( quantity ) AS qty
FROM `sales`
WHERE year( created ) = '2013'
GROUP BY monthname( created )
ORDER BY monthname( created ) DESC
It gives the result:
month | qty
______________________
January | 10
February | 20
April | 10
But what I was trying to retrieve is:
month | qty
______________________
January | 10
February | 20
March | 0
April | 10
- Since I have no sales no march the result must return march sales with quantity 0.
I am using Codeigniter in my application so if we can't solve it through sql then may be you can show me the way to solve it through Codeigniter.
$this->db->select('monthname(created) as month, sum(quantity) as qty'); $this->db->from('sales'); $this->db->where('year(created) = 2013'); $this->db->group_by('monthname(created)'); $this->db->order_by('monthname(created)'); $this->data['sales'] = $this->db->get()-result();
In the view:
$data = array();
foreach($sales as $sale) {
$data[] = $sale->qty;
}
Output of $data
:
10, 20, 10 in array;
what I need is
10, 20, 0, 10 array