2

Is it ever possible to have a query that selects rows grouped by the day or month from a table that has a date_time filed holding full date_time value?

enter image description here

I need to get the sum of sr13, r4, huawei for each day. Any idea of how I can do it would be heartfully appreciated!

2
  • do you have any filters to restrict data to a month? If you group by day then you might get rows from different months for same day. Commented Mar 12, 2013 at 5:18
  • $counter_result = $this->db->query("select date_time, sum(r4) as sum_r4, sum(sr13) as sum_sr13, sum(huawei) as sum_huawei from cdrcount where date_time like '2013-03-%' group by date_time"); Commented Mar 12, 2013 at 5:24

1 Answer 1

2

You can use DATE() to extract the date from the value,

SELECT  DATE(date_time) datetime,
        SUM(sr13) total_sr13,
        SUM(r4) total_4
FROM    TableName
GROUP   BY DATE(date_time)
Sign up to request clarification or add additional context in comments.

3 Comments

What if I want to group by month.. to get the sum of the three(r4,sr13,huawei) for each month?
Bingo.. I got it..: GROUP BY month(date_time);
you can use either MONTH() or MONTHNAME() together with YEAR(), ex SELECT YEAR(date_time), MONTHNAME(date_time),.... GROUP BY YEAR(date_time), MONTHNAME(date_time)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.