每组的百分比数

时间:2014-07-03 14:06:17

标签: mysql count group-by sum percentage

我有以下场景,包括进入场地的人数和类型,四舍五入到一小时。

Date-time              Type
2014-01-01 01:00:00    Employee
2014-01-01 01:00:00    Visitor
2014-01-01 01:00:00    Employee
2014-01-01 02:00:00    Visitor
2014-01-01 02:00:00    Visitor
2014-01-01 02:00:00    Visitor
2014-01-01 02:00:00    Employee
2014-01-01 03:00:00    Employee
2014-01-01 03:00:00    Visitor
2014-01-01 03:00:00    Employee

我正在进行查询以将它们组合在一起

select 
    count(*), 
    HOUR(Date-Time), 
    Type 
from table1 
where Date(Date-time)='2014-01-01' 
group by HOUR(Date-Time), Type; 

这给了我以下细分

count(*)  HOUR Type
  2         1   Employee
  1         1   Visitor 
  1         2   Employee 
  3         2   Visitor
  2         3   Employee
  1         3   Visitor

我试图根据每小时的总计数来获得进入建筑物的员工类型的百分比。因此,对于第1小时,共有3人进入场所,员工的百分比为(2/3 * 100)= 66.66%,访客将为(1/3 * 100)= 33.33%

我想要的输出是

count(*)  HOUR Type        Percentage
  2         1   Employee   66.66
  1         1   Visitor    33.33 
  1         2   Employee   25.00
  3         2   Visitor    75.00
  so on....

0 个答案:

没有答案