Home > Enterprise >  Calculate a percentage of column by userid (SQL)
Calculate a percentage of column by userid (SQL)

Time:12-28

I have this really small table where I need to calculate what percent of users used a time-off feature? time_off_used parameters show if users used that feature (1), or not (0)

role user_id time_off_used
Admin 45ds4f5d4sfd5s5fd5s4dfg 0
Admin 45ds5f4ds5sdf45dfs5sdgfgf 1
Regular 45ds4f5d4sfd5s5fd5s4dfg 0
Admin dh454hkd564khdg564kd6fgjg 1
Project manager sgj3js32jgfsj1s2jss1jgssg54 1
Admin 45ds5f4ds5sdf45dfs5sdgfgf 1
Regular 45d34frty5d4sfd5s5fd5s4dfg 0
Admin dhy556gd564khdg564kd6fgjg 1
Project manager sgj3js32jgfsj1s2jss1jgssg54 1

I need just one number as a result, for example 70.

CodePudding user response:

simple as this :

select sum(time_off_used)* 100.0 / count(*) as percentage_used_feature
from tablename 
  •  Tags:  
  • sql
  • Related