简体   繁体   中英

List all employees having percentage greater than some number

How to list all employees having percentage greater than some number, and percentage gets calculated on the basis of max salary.

Example:

if max salary is 100 and an employee salary is 50 then percentage should show 50.

This is what i tried:

select (salary/Max(salary)*100) as percentage from test.employeetable
where percentage > 75;

表

Error that i get is:

Unknown column 'percentage' in 'where clause

Try something like that;

select * from (
select (salary / (select max(salary) from test.employeetable) * 100) as percentage from test.employeetable) Records
where percentage > 75;

First get the max in a variable, then select the relevant results

select @max := max(salary) from test.employeetable;
select (salary/@max*100) as pc from test.employeetable having pc > 75;

note the having instead of where .

You could display the relevant salary (etc...) as well

select @max := max(salary) from test.employeetable;
select salary,(salary/@max*100) as pc from test.employeetable having pc > 75;

Without using a variable

select (salary/m.mx*100) as pc from test.employeetable, 
(select max(salary) as mx from test.employeetable) as m
having pc > 75;
Select (salary/Max(salary)*100) as percentage 
from test.employeetable 
where (salary/Max(salary)*100) > 75;  

in MySql you Can not use an "as" in where clause

Use a subquery

SELECT *
FROM (
  SELECT (salary / MAX(salary) * 100) AS percentage
  FROM test.employeetable
) x
WHERE percentage > 75

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM