MySQL show records month wise

蹲街弑〆低调 提交于 2020-05-04 05:33:17

问题


I have some records in my Database table.

User passes the start and the end date and then expects an output. If a user passes the start and end date for same month then it should show only one record, but if the end date is extended one or more month then it should displays month wise record.

For now I am able to collect total data while following this query.

SELECT m.data_date_time
     , SUM(m.kwh) total_kwh
     , m.cust_id Customer_ID
     , m.msn
  FROM mdc_meters_data m 
 WHERE m.data_date_time >= '2020-04-01 00:00:00' 
   AND m.data_date_time <= '2020-05-31 00:00:00'

Output

Data_Date_Time      == total_kwh == Customer_ID == MSN
2020-04-01 11:44:13 || 95585     || 37010114707 || 7898985212

Expected Output

As the month end date is 05 so it should show two records like below

Data_Date_Time      == total_kwh == Customer_ID == MSN
2020-04-01 11:44:13 || 50000     || 37010114707 || 7898985212
2020-05-01 10:25:05 || 45585     || 37010114707 || 7898985212

I have tried to put GroupBy data_date_time but it doesn't helped me out.

How can I do this ?

Any help would be highly appreciated.


回答1:


SELECT m.data_date_time AS 'Data_Date_Time', SUM(m.kwh) AS total_kwh, m.cust_id AS 'Customer_ID', m.msn AS MSN, date_format(data_date_time, "%Y-%m") as monYear FROM mdc_meters_data m WHERE m.data_date_time >= 2020-04-01 00:00:00' AND m.data_date_time`<='2020-05-31 00:00:00' group by monYear

Hope it will help you for getting idea




回答2:


please check this answer

    SELECT m.`data_date_time` AS 'Data_Date_Time', SUM(m.`kwh`) AS total_kwh, m.`cust_id` AS 'Customer_ID', m.`msn` AS MSN 
FROM table AS r JOIN (
        SELECT MAX(m.`data_date_time`) AS data_date_time
        FROM table AS t
        GROUP BY YEAR(m.`data_date_time`), MONTH(t.data_date_time)
    ) AS x USING (data_date_time)

`mdc_meters_data` m WHERE m.`data_date_time`>='2020-04-01 00:00:00' AND m.`data_date_time`<='2020-05-31 00:00:00'



回答3:


You will have to use GROUP BY the correct manner.

SELECT DATE_FORMAT('m.`data_date_time`, '%Y-%m') AS Data_Date_Time, 
       SUM(m.`kwh`) AS total_kwh, 
       m.`cust_id` AS Customer_ID,
       m.`msn` AS MSN 
FROM `mdc_meters_data` m 
WHERE m.`data_date_time`>='2020-04-01 00:00:00' AND m.`data_date_time`<='2020-05-31 23:59:59'
GROUP BY Customer_ID, Data_Date_Time, MSN

Now the problem with the above query is that you don't need to group your results by MSN

One workaround is to use any_value function around your nonaggregated MSN columns in the select list. This will just pick any MSN value from the matching list and make sure you will still have only 2 entries shown in the results.

SELECT any_value(DATE_FORMAT('m.`data_date_time`, '%Y-%m')) AS Data_Date_Time, 
       SUM(m.`kwh`) AS total_kwh, 
       m.`cust_id` AS Customer_ID,
       any_value(m.`msn`)
FROM `mdc_meters_data` m 
WHERE m.`data_date_time`>='2020-04-01 00:00:00' AND m.`data_date_time`<='2020-05-31 23:59:59'
GROUP BY Customer_ID, Data_Date_Time

Also, note how the 31st of May has been included in your query by giving by extending the range to the time 23:59:59.



来源:https://stackoverflow.com/questions/61554627/mysql-show-records-month-wise

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!