Percentage Per Group From Count

2019-09-09 07:54发布

问题:

I have the following scenario of the number of people and the type entering the premises, rounded to an hour.

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

I am doing a query to group them together

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

This gives me the following breakdown

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

I am trying to get a percentage of the type of employee who entered the building based on the total number of count per hour. So for hour 1, there were a total of 3 people who entered the premises, The percentage for employee would be (2/3*100)=66.66% for visitor would be (1/3*100)=33.33%

The output I want is

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....