业务背景:首先对sd.project_name项目名称进行分组后,但是分组聚合的GROUP_CONCAT的start_time和end_time组合后乱序了,导致的sql结果是这样子的
这个有还无剧场最早的一个场次时间是10:30,按道理讲他应该是排在第一个位置。
但是group by 之后分组就乱序了,因此需要对组内的start_time进行升序排序,
使用GROUP_CONCAT之后然后再使用 ORDER BY sd.start_time ASC SEPARATOR ', '进行排序即可
SELECT
sd.project_name as projectName,
sd.project_type as projectType,
sd.project_time as projectTime, GROUP_CONCAT(DATE_FORMAT(sd.start_time,'%H:%i'),'#',DATE_FORMAT(sd.end_time,'%H:%i') ORDER BY sd.start_time ASC SEPARATOR ', ') as startTime,
IFNULL(sum(pd.book_num),0) as bookNum,
IFNULL(ROUND(sum(pd.check_num)/sum(pd.capacity_num)*100,1),0) as seatRatio
FROM
r_venue_management_scheduling_detail sd
left JOIN r_venue_management_project_day pd ON sd.project_name = pd.project_name and pd.enter_date=CURRENT_DATE()
WHERE
sd.start_time >= '2023-07-25 00:00:00'
AND sd.end_time <= '2023-07-25 23:59:59'
AND sd.dept_id = '001003'
and sd.status=1
GROUP BY sd.project_name
order by sd.project_type,startTime