接前文
前文有一块写多余了
纠正如下

王工优化的版本如下,确实是效率又高,可读性又好,整体还更精炼.
- select
- case
- when starttime is null then '汇总'
- else ''
- end l,
- ifnull(starttime, minstarttime) starttime,
- ifnull(endtime, maxendtime) endtime,
- `中奖数量`,`抽奖数量`
- from(
- select
- ceil(t1.id/15) l,
- starttime,
- endtime,
- sum(ifnull(`中奖数量`,0)) `中奖数量`,
- sum(ifnull(`抽奖数量`,0)) `抽奖数量`,
- min(starttime) minstarttime,
- max(endtime) maxendtime
- from (
- select
- id,
- '2017-12-21 09:30:00'+ interval (id-1) minute starttime,
- '2017-12-21 09:30:59'+ interval (id-1) minute endtime
- from nums,
- (select @rn:=0,@starttime:='',@endtime:='',@c1:=-1,@c2:=-1) vars
- where id<=10000
- AND
- ('2017-12-21 09:30:00'+ interval (id-1) minute)<=
- (select max(actiontime)+interval '15' minute FROM award_gift_record WHERE awardactId=235)
- ) t1
- left join
- (
- SELECT
- date_format(actiontime,'%Y-%m-%d %H:%i:00') 时间,
- count(case when AwardGiftID!=-1 then 1 else null end) 中奖数量,
- count(AwardGiftID) 抽奖数量
- FROM award_gift_record
- WHERE awardactId=235
- group by date_format(actiontime,'%Y-%m-%d %H:%i:00')
- ) t2 on(t2.时间 between t1.starttime and endtime)
- group by l,starttime,endtime with rollup having
- (
- (endtime is null and starttime is null)
- or
- (starttime is not null and endtime is not null)
- )
- ) result;