Home > database >  Mysql for the recent half a year last upload record every month
Mysql for the recent half a year last upload record every month

Time:09-30

A building has a lot of water meter, every day is not time to upload data, I want to get every month each meter table and a half years last upload data



SELECT
DATE_FORMAT (CREAT_DATE, '% % Y - m),
The SUM (PROPERTY_VALUE)
The FROM
(SELECT
*
The FROM
S_water
WHERE CREAT_DATE BETWEEN DATE_SUB (NOW (), INTERVAL 5 MONTH)
AND NOW ()) AS a
GROUP BY DATE_FORMAT (CREAT_DATE, '% Y - % m')
So calculate words will elevate the monthly data are calculated, I don't want to filter out the last data

CodePudding user response:

Select *
The from TB A
Where not the exists (select 1 from TB where device_code=a. d. evice_code and create_date & lt; A.c reate_date)

CodePudding user response:

Select the id FROM s_water where CREAT_DATE & gt; DATE_SUB (NOW (), INTERVAL 6 MONTH) group by id, DATE_FORMAT (CREAT_DATE, '% Y - % m) order by CREAT_DATE desc

So you can get a month in the end, a record ID
  • Related