實用SQL統計搖獎結果

2021-09-02 10:44:14 字數 1138 閱讀 8686

下面是搖獎概率調整後,一條實用的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語句

統計勝負結果的sql語句,這裡採用子查詢來實現。表結構和資料 date result 2011 02 01 勝 2011 02 01 負 2011 02 01 勝 2011 02 02 勝 2011 02 02 負 採用子查詢 select date,select count from table1...

Java Web 實用返回結果封裝

實用的返回結果封裝 author huangwenjun description date created in 13 59 2018 3 28 restcontroller crossorigin public class configdemocontroller public resultbul...

實用mysql統計函式

按固定週期統計 先看一條完整的sql語句 select date format date add startdate,interval 8 hour u as period,count 1 as total from table1 group by period 解釋 date add 時間增加,例...