MySQL按照操作时间半小时等分时间段统计

难点:

业务需求按照操作时间进行等分时间段进行统计半小时的进线量等数据,中间会存在空白的操作时间,将不能讲24小时分割成48段
具体的数据如下:
MySQL按照操作时间半小时等分时间段统计_第1张图片
截图可以看出12月30号的数据从2019-12-30 10:19:32开始,之前是没有操作记录的

1. 可在对应的数据库中新建一张临时表

将24小时分割的48段存入表中,进行left join(针对已上线项目,这一操作不是最优的解决办法)

2. 在SQL实现,如下

SELECT DATE_FORMAT(rs.operation_time, '%Y-%m-%d') '日期',
	t.time '时间',
	IFNULL(rs.inLineCount, 0) '进线总量',
	IFNULL(rs.receptionCount, 0) '所有坐席接起总量',IFNULL(rs.missCount,0) '进线量(流转次数>1)'
FROM
	(
		SELECT '00:00-00:30' time union
		SELECT '00:30-01:00' time union
		SELECT '01:00-01:30' time union
		SELECT '01:30-02:00' time union
		SELECT '02:00-02:30' time union
		SELECT '02:30-03:00' time union
		SELECT '03:00-03:30' time union
		SELECT '03:30-04:00' time union
		SELECT '04:00-04:30' time union
		SELECT '04:30-05:00' time union
		SELECT '05:00-05:30' time union
		SELECT '05:30-06:00' time union
		SELECT '06:00-06:30' time union
		SELECT '06:30-07:00' time union
		SELECT '07:00-07:30' time union
		SELECT '07:30-08:00' time union
		SELECT '08:00-08:30' time union
		SELECT '08:30-09:00' time union
		SELECT '09:00-09:30' time union
		SELECT '09:30-10:00' time union
		SELECT '10:00-10:30' time union
		SELECT '10:30-11:00' time union
		SELECT '11:00-11:30' time union
		SELECT '11:30-12:00' time union
		SELECT '12:00-12:30' time union
		SELECT '12:30-13:00' time union
		SELECT '13:00-13:30' time union
		SELECT '13:30-14:00' time union
		SELECT '14:00-14:30' time union
		SELECT '14:30-15:00' time union
		SELECT '15:00-15:30' time union
		SELECT '15:30-16:00' time union
		SELECT '16:00-16:30' time union
		SELECT '16:30-17:00' time union
		SELECT '17:00-17:30' time union
		SELECT '17:30-18:00' time union
		SELECT '18:00-18:30' time union
		SELECT '18:30-19:00' time union
		SELECT '19:00-19:30' time union
		SELECT '19:30-20:00' time union
		SELECT '20:00-20:30' time union
		SELECT '20:30-21:00' time union
		SELECT '21:00-21:30' time union
		SELECT '21:30-22:00' time union
		SELECT '22:00-22:30' time union
		SELECT '22:30-23:00' time union
		SELECT '23:00-23:30' time union
		SELECT '23:30-00:00' time
	) t
LEFT JOIN (
	SELECT
		sum(
			CASE
			WHEN csr.operation_type = 1 THEN
				1
			END
		) inLineCount,
		sum(
			CASE
			WHEN csr.operation_type = 4 THEN
				1
			END
		) receptionCount,
		sum(
			CASE
			WHEN csr.operation_type = 3 THEN
				1
			END
		) missCount,
		CASE
	WHEN DATE_FORMAT(csr.operation_time, '%i') > 30
	AND DATE_FORMAT(csr.operation_time, '%i') <= 59 THEN
		concat(
			DATE_FORMAT(csr.operation_time, '%H:30'),
			'-',
			DATE_FORMAT(
				DATE_ADD(
					csr.operation_time,
					INTERVAL 1 HOUR
				),
				'%H:00'
			)
		)
	ELSE
		concat(
			DATE_FORMAT(csr.operation_time, '%H:00'),
			'-',
			DATE_FORMAT(csr.operation_time, '%H:30')
		)
	END period,csr.operation_time
	FROM
		customer_status_record csr USE INDEX (
			idx_customerstatusrecord_operation_time
		)
	INNER JOIN customer_info ci ON csr.customer_id = ci.customer_id 
	and ci.enterprise_id <> 1
	GROUP BY
		period
	ORDER BY
		csr.operation_time DESC
) rs ON t.time = rs.period

通过上述的拼接满足对应的业务需求

你可能感兴趣的:(MySQL)