How to calculate Quantile 95 in Oracle SQL - sql

How to calculate Quantile 95 value, using SQL or PL/SQL?

With the SQL aggregate or analytic function PERCENTILE_DISC() (or perhaps PERCENTILE_CONT()). Google for your version of Oracle and the function name to find the Oracle documentation for these functions.
Here is how I can find the 95th percentile salary in the HR.EMPLOYEES table (HR is the standard HR schema that is installed with many Oracle databases):
select percentile_disc(0.95) within group (order by salary) as sal_95th_pctile
from hr.employees
;
SAL_95TH_PCTILE
---------------
13000
If instead I wanted to find the 95th percentile salary in each department, I would use the analytic version:
select percentile_disc(0.95) within group (order by salary)
over (partition by department_id) as sal_95th_pctile
from hr.employees
;
For the HR.EMPLOYEES table this makes little sense, since each department has only a few employees, so "95th percentile" is meaningless; but that's how you would do it when all "departments" had many values from which to compute the 95th percentile.

Related

Computing the median of salaries under each manager in BigQuery SQL

I have a BigQuery table with columns: employee, salary, gender, manager. I would like to compute the median, within each team (so, for each manager), of female employees' salaries.
I have tried using the PERCENTILE_CONT(..., 0.5) navigation function but it seems it does not support GROUP BY
This is my query:
SELECT
manager,
PERCENTILE_CONT(salary,
0.5) OVER() AS median_of_women_salaries
FROM
employees_table
WHERE
gender = 'woman'
GROUP BY
manager
What I get is the error message:
"SELECT list expression references column salary which is neither grouped nor aggregated at [.:.]"
As a result, I would like to get a table with columns manager and median_of_women_salaries that would show the median of females salaries under each manager.
Thank you very much for your help!
You could use an existing shared UDF:
SELECT
manager,
fhoffa.x.median(ARRAY_AGG(salary)) AS median_of_women_salaries
FROM employees_table
WHERE gender = 'woman'
GROUP BY manager
https://medium.com/#hoffa/new-in-bigquery-persistent-udfs-c9ea4100fd83
https://console.cloud.google.com/bigquery?p=fhoffa&d=x&r=median&page=routine

Using ROUND, AVG and COUNT in the same SQL query

I need to write a query where I need to first count the people working in a department, then calculate the average people working in a department and finally round it to only one decimal place. I tried so many different variations.
That's what I got so far although it's not the first one I tried but I always get the same error message. (ORA-00979 - not a group by expression)
SELECT department_id,
ROUND(AVG(c.cnumber),1)
FROM employees c
WHERE c.cnumber =
(SELECT COUNT(c.employee_id)
FROM employees c)
GROUP BY department_id;
I really don't know what do to at this point and would appreciate any help.
Employees Table:
Try this (Oracle syntax) example from your description:
with department_count as (
SELECT department_id, COUNT(c.employee_id) as employee_count
FROM employees c
group by department_id
)
SELECT department_id,
ROUND(AVG(c.employee_count),1)
FROM department_count c
GROUP BY department_id;
But this query not make sense. Count is integer, and count return one number for one department in this case AVG return the same value as count.
Maybe you have calculate number of employee and averange of salary on department?

Oracle 11g: Write a query that lists the highest earners for each department

This is a problem I've spent hours on now, and tried various different ways. It HAS to use Subqueries.
"Write a query that lists the highest earners for each department. Include the last_name, department_id, and the salary for each employee."
I've done a ton of subquery methods, and nothing works. I either get an error, or "No rows return". I'm assuming because one of the department_id is null, but even with NVL(department_id), I'm still having trouble. I tried splitting the table, and had no luck. Textbook's no help, my instructor is kind of useless, please... any help at all.
Here's a snapshot of the values, if that helps.
https://www.dropbox.com/s/bxtntlzqixdizzp/helpme.png?dl=0
You can rank the values within each department - then pull only the first place ranks in the outer query.
select a.last_name
,a.department_id
,a.salary
from (
select last_name
,department_id
,salary
,rank() over (partition by department_id order by salary desc) as rnk
from tablename
) a
where rnk=1
The partition groups all employees together who share the same department and should work regardless of the null value.
After grouping them - the order by tells that group to order on salary descending, and give a rank. You can run just the inner query to get an idea of what it does.

SQL Average calculation

Let's say there is an employer entity and it has an attribute of salary. And employer works in a department which is located in Oxford Building.(Building address is unique for building)
How can I write a SQL query that computes the average salary per building? Does it mean that salary is foreign key or what? Is it correct, if I do just
SELECT AVG(Salary) AS Averagesalary FROM Employee;
but in that case there will be no Building
That's why there's GROUP BY, to specify how you want to "group" your records. Right now you're doing an average of ALL records in table.
SELECT Building, AVG(SALARY)
FROM Employee
GROUP BY Building
The above query will also average all records, but do it per-building.

How to calculate Standard Deviation in Oracle SQL Developer?

I have a table employees,
CREATE TABLE employees (
employeeid NUMERIC(9) NOT NULL,
firstname VARCHAR(10),
lastname VARCHAR(20),
deptcode CHAR(5),
salary NUMERIC(9, 2),
PRIMARY KEY(employeeid)
);
and I want to calculate Standard Deviation for salary.
This is the code I am using:
select avg(salary) as mean
,sqrt(sum((salary-avg(salary))*(salary-avg(salary)))/count(employeeid)) as SD
from employees
group by employeeid;
I am getting this error:
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 260 Column: 12
Line 260 Column 12 is avg(salary)
How can I sort this out?
Oracle has a built-in function to calculate standard deviation: STDDEV.
The usage is as you'd expect for any aggregate function.
select stddev(salary)
from employees;
I'd just use the stddev function
SELECT avg(salary) as mean,
stddev(salary) as sd
FROM employees
It doesn't make sense to group by employeeid since that is, presumably unique. It doesn't make sense to talk about the average salary by employee, you want the average salary across all employees (or all departments or some other aggregatable unit)
The salary-avg(salary) can't be evaluated; avg(salary) is not available during execution of the query but only after all records are retrieved.
I would suggest to add AVG calculations in a subquery and JOIN it to the main one
select avg(salary) as mean,
sqrt(sum((salary-avg_res.avg)*(salary-avg_res.avg))/count(employeeid)) as SD
from employees JOIN
(select employeeid,avg(salary) as avg
from employees
group by employeeid) avg_res ON employees.employeeid=avg_res.employeeid
group by employeeid;
I thought you had to include the column in the GROUP BY in the SELECT:
select employeeid
,avg(salary) as mean
,sqrt(sum((salary-avg(salary))*(salary-avg(salary)))/count(employeeid)) as SD
from employees
group by employeeid;
But on further reflection the query doesn't make much sense unless it's historical data. An employee id ought to be unique to a single employee. Unless this is an average over time there should be only one salary per employee. Your mean will be the salary and the standard deviation will be zero.
A better query might be average of all salaries. In that case, remove the GROUP BY.
One more nitpick: the formula you're using is more properly called the population standard deviation. The sample deviation divides by (n-1).