mysql分组统计百分比

 SELECT
			t2.time statisName,
			ROUND(T1.co / T2.totalCo * 100, 1) value
		FROM
			(
				SELECT
					DATE_FORMAT(CREATE_TIME, '%Y-%m') time,
					COUNT(*) AS co
				FROM
					t_event
				WHERE
					t_event.EVENT_STATUS = #{eventStatus}
				AND t_event.CREATE_TIME >= #{beginDate}
				AND t_event.CREATE_TIME <= #{endDate}
					
				GROUP BY
					DATE_FORMAT(CREATE_TIME, '%Y-%m')
			) T1,
			(
				SELECT
					DATE_FORMAT(CREATE_TIME, '%Y-%m') time,
					COUNT(*) AS totalCo
				FROM
					t_event
				WHERE
					t_event.CREATE_TIME >= #{beginDate}
				AND t_event.CREATE_TIME <= #{endDate}
				
				GROUP BY
					DATE_FORMAT(CREATE_TIME, '%Y-%m')
			) T2

 

你可能感兴趣的:(mysql)