Mysql根据时间排序后再分组查询解决方案

场景一

在一张表内,有字段number来区分设备编号。字段create_time表示入库时间。现在需要查询近15天中,每天某个设备最新的数据。

SELECT
	max( date( aa.create_time ) ) AS create_time,
	aa.number AS number,
	aa.state
FROM
	(
	SELECT CONVERT
		(
			a.create_time,
		CHAR ( 10 )) datetemp,
		a.create_time,
		a.number,
		a.state
	FROM
		表名 a 
	WHERE
		DATE_SUB( CURDATE(), INTERVAL 15 DAY ) <= date( a.create_time ) 
	GROUP BY
		a.create_time 
	ORDER BY
		a.id DESC 
	) AS aa 
GROUP BY
	aa.datetemp

场景二

在一张表内,字段create_time表示入库时间,字段power【i】表示水表的度数,一共15个水表,字段floor表示楼层数。现在需要查询每层楼各水表的日报、周报和月报。
日报:近15天内,每日用水量的统计(用水量为当天最后一条数据水表读数-当天最新一条水表的度数),按日分组
周报:近15周内,每周用水量的统计(用水量为本周最后一条数据水表读数-本周最新一条水表的度数),按周分组
月报:近一年内,每月用水量的统计(用水量为本月最后一条数据水表读数-本月最新一条水表的度数),按月分组

### 入参floor为楼层,type为查询为日报还是周报还是月报
### 为日报时,format='%Y-%m-%d'
### 为周报时,format='%Y-%u'
### 为月报时,format='%Y-%m'
SELECT
	a.floor,
	a.water_no1 - b.water_no1 water_no1,
	a.water_no2 - b.water_no2 water_no2,
	a.water_no3 - b.water_no3 water_no3,
	a.water_no4 - b.water_no4 water_no4,
	a.water_no5 - b.water_no5 water_no5,
	a.water_no6 - b.water_no6 water_no6,
	a.water_no7 - b.water_no7 water_no7,
	a.water_no8 - b.water_no8 water_no8,
	a.water_no9 - b.water_no9 water_no9,
	a.water_no10 - b.water_no10 water_no10,
	a.water_no11 - b.water_no11 water_no11,
	a.water_no12 - b.water_no12 water_no12,
	a.water_no13 - b.water_no13 water_no13,
	a.water_no14 - b.water_no14 water_no14,
	a.water_no15 - b.water_no15 water_no15,
	a.type
FROM(
	SELECT
		t.*
	FROM( 
		SELECT
			DATE_FORMAT(create_time,${format}) as type,
			表名.* 
		FROM 表名
		<where>
			<if test="cm.floor != null and cm.floor != ''">
				and floor=#{cm.floor}
			</if>
			<if test="cm.type =='day'">
				and date_sub(curdate(), interval 15 DAY) <![CDATA[<=]]> date(create_time)
			</if>
			<if test="cm.type =='week'">
				and date_sub(curdate(), interval 15 WEEK) <![CDATA[<=]]> date(create_time)
			</if>
			<if test="cm.type =='month'">
				and  date_sub(curdate(), interval 1 YEAR) <![CDATA[<=]]> date(create_time)
			</if>
		</where>
		HAVING 1 ORDER BY create_time DESC ) t
		GROUP BY
			t.floor,type
		) a
	LEFT JOIN (
	SELECT
		t.*
	FROM( 
		SELECT
			DATE_FORMAT(create_time,${format}) as type,
			表名.* 
		FROM 表名
		<where>
			<if test="cm.floor != null and cm.floor != ''">
				and floor=#{cm.floor}
			</if>
			<if test="cm.type =='day'">
				and date_sub(curdate(), interval 15 DAY) <![CDATA[<=]]> date(create_time)
			</if>
			<if test="cm.type =='week'">
				and date_sub(curdate(), interval 15 WEEK) <![CDATA[<=]]> date(create_time)
			</if>
			<if test="cm.type =='month'">
				and  date_sub(curdate(), interval 1 YEAR) <![CDATA[<=]]> date(create_time)
			</if>
		</where>
		HAVING 1 ORDER BY create_time ASC ) t
		GROUP BY
			t.floor,type
		) b 
	ON a.floor = b.floor and a.type = b.type

根据日期查询

-- 今天
select * from 表名 where to_days(时间字段名) = to_days(now());

--昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

--本周
SELECT * FROM 表名 WHERE YEARWEEK( date_format( 时间字段名,'%Y-%m-%d' ) ) = YEARWEEK( now() ) ;

--本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m' )

--上一个月
select * from 表名 where date_format(时间字段名,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')

--本年
SELECT * FROM 表名 WHERE YEAR( 时间字段名 ) = YEAR( NOW( ) )

--查询距离当前现在6个月的数据
select  * from 表名 where 时间字段名 between date_sub(now(),interval 6 month) and now();

你可能感兴趣的:(Mysql,mysql,数据库,sql)