0

I have table like this:

date_time               value
2015-06-20 10:00:00     10
2015-06-20 9:00:00      6
2015-06-21 6:00:00      5
2015-06-22 11:00:00     12
2015-06-22 10:00:00     11
2015-06-23 4:00:00      20

I want to get the total value of each date Currently I have this on CodeIgniter but is not working:

$this->db->group_by('date_time');
$query = $this->db->get('table_a');

Any idea to fix. Thanks.

3
  • so what is Table a and what is table B?? upload your table code or structurer Jun 23, 2015 at 3:43
  • don't mind other join tables.
    – Cris
    Jun 23, 2015 at 5:10
  • give us your table structurer for best solution Jun 23, 2015 at 5:13

2 Answers 2

3

You need to ditch the star '*' and specify the expressions you want to return. To get a total, you need an aggregate function, and to get just the date portion of date_time, you can use the DATE() function.

   $this->db->select('DATE(a.date_time) AS date, SUM(a.value) AS total_value');

And you want to GROUP BY DATE(a.date_time).

   $this->db->group_by('DATE(a.date_time)');
0
-2
$result = $query->result();

Try with add this line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.