每隔15行生成一個匯總行

用戶抽獎模塊
award_gift_record 是用戶抽獎記錄表.
其中actiontime 是抽獎時間.
AwardGiftID表示中獎的禮物ID. -1表示沒有中獎
awardactId 表示活動ID

需求:查詢每分鐘抽獎人數(shù)和中獎人數(shù)

  • create table nums(id int not null primary key);

  • delimiter $$
  • create procedure pCreateNums(cnt int)
  • begin
  •     declare s int default 1;
  •     truncate table nums;
  •     while s<=cnt do
  •         insert into nums select s;
  •         set s=s+1;
  •     end while;
  • end $$
  • delimiter ;

  • delimiter $$
  • create procedure pFastCreateNums(cnt int)
  • begin
  •     declare s int default 1;
  •     truncate table nums;
  •     insert into nums select s;
  •     while s*2<=cnt do
  •         insert into nums select id+s from nums;
  •         set s=s*2;
  •     end while;
  • end $$
  • delimiter ;

  • 初始化數(shù)字輔助表
    call pFastCreateNums(100000);

    數(shù)字輔助表詳見:
    http://blog.itpub.net/29254281/viewspace-1362897/

    思路就是用數(shù)字輔助表, 活動期間每分鐘都生成一條記錄.然后用左連接 匹配有抽獎中獎的記錄

  • select if(mod(result.id,16)=0,'匯總','') 匯總,result.starttime,result.endtime,result.`中獎數(shù)量`,result.`抽獎數(shù)量` from (  
  • select   
  •     n.id,  
  •     when @starttime='' then @starttime:=starttime end ,  
  •     when mod(n.id,16)!=0 then @endtime:=endtime end,  
  •     if(mod(n.id,16)!=0 ,when @c1=-1 then @c1:=`中獎數(shù)量` else @c1:=@c1+`中獎數(shù)量` end,''),  
  •     if(mod(n.id,16)!=0 ,when @c2=-1 then @c2:=`抽獎數(shù)量` else @c2:=@c2+`抽獎數(shù)量` end,''),  
  •     when mod(n.id,16)=0 then @starttime else starttime end starttime,  
  •     when mod(n.id,16)=0 then @endtime else endtime end endtime,  
  •     when mod(n.id,16)=0 then @c1 else `中獎數(shù)量` end `中獎數(shù)量`,  
  •     when mod(n.id,16)=0 then @c2 else `抽獎數(shù)量`  end `抽獎數(shù)量`,  
  •     when mod(n.id,16)=0 then @starttime:='' else null end ,  
  •     when mod(n.id,16)=0 then @endtime:='' else null end ,  
  •     when mod(n.id,16)=0 then @c1:=-1 else null end ,  
  •     when mod(n.id,16)=0 then @c2:=-1 else null end   
  •       
  • from  
  •     nums n  
  •     join(  
  •         select t3.*,when mod(@rn+1,16)=0 then @rn:=@rn+2 else @rn:=@rn+1 end rn from (  
  •             select starttime,endtime,ifnull(`中獎數(shù)量`,0) `中獎數(shù)量`,ifnull(`抽獎數(shù)量`,0) `抽獎數(shù)量`  
  •             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  
  •             join   
  •             (  
  •                 SELECT   
  •                 date_format(actiontime,'%Y-%m-%d %H:%i:00') 時間,  
  •                 when AwardGiftID!=-1 then 1 else null end) 中獎數(shù)量,  
  •                 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 starttime,endtime  
  •             order by starttime  
  •         ) t3   
  •     ) t4 on(n.id=t4.rn)  
  • where  
  •     n.id <= (  
  •         select   
  •             ceil(timestampdiff(MINUTE,  
  •                             min(actiontime),  
  •                             max(actiontime) + interval '15' minute) / 15 * 16) + 15  
  •         FROM  
  •             award_gift_record  
  •         WHERE  
  •             awardactId = 235)  
  • ) result;  



  • 這里最核心的是,每15行生成一行,然后用自定義變量填充生成行.

    在需求二的結(jié)果上 添加行號。這個行號每到 模16 就 加二。
    case when mod(@rn+1,16)=0 then @rn:=@rn+2 else @rn:=@rn+1 end rn

    這樣行號會是這樣
    ...
    14
    15
    17
    18

    中間把16 空過去.
    這樣再用數(shù)字輔助表左連接這個結(jié)果。 就可以每15行多生成一行了。

    最后通過自定義變量的運(yùn)算,填充生成的行即可。

    每隔15行生成一個匯總行



    本文名稱:每隔15行生成一個匯總行
    文章分享:http://bm7419.com/article42/jcioec.html

    成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、建站公司網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名、企業(yè)建站

    廣告

    聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)