实用SQL统计摇奖结果

  下面是摇奖概率调整后,一条实用的SQL统计查看结果:

 

   该SQL非常高效,绝对实用,不用group by 关键是用到case这个函数。

 

SELECT  '2015-04-27  老的摇奖概率  1-11点数据',
   SUM(CASE t.result WHEN 1 THEN 1 ELSE 1 END) AS 总摇奖人数, 
   SUM(CASE t.result WHEN 1 THEN 1 ELSE 0 END) AS 一等奖, 
   SUM(CASE t.result WHEN 2 THEN 1 ELSE 0 END) AS 二等奖, 
   SUM(CASE t.result WHEN 3 THEN 1 ELSE 0 END) AS 三等奖,
   SUM(CASE t.result WHEN 0 THEN 1 ELSE 0 END) AS 未中奖
FROM mms.mobile_ernie t 
where ernie_time>='2015-04-27 00:00:00' and ernie_time<='2015-04-27 11:00:00'
union all
SELECT  '2015-04-28  摇奖概率调整后  1-11点数据',
   SUM(CASE t.result WHEN 1 THEN 1 ELSE 1 END) AS 总摇奖人数, 
   SUM(CASE t.result WHEN 1 THEN 1 ELSE 0 END) AS 一等奖, 
   SUM(CASE t.result WHEN 2 THEN 1 ELSE 0 END) AS 二等奖, 
   SUM(CASE t.result WHEN 3 THEN 1 ELSE 0 END) AS 三等奖,
   SUM(CASE t.result WHEN 0 THEN 1 ELSE 0 END) AS 未中奖
FROM mms.mobile_ernie t 
where ernie_time>='2015-04-28 00:00:00' and ernie_time<='2015-04-28 11:00:00'

 

查询结果截图如下:



 

 

你可能感兴趣的:(sql)