Clone and init the database (requires a local PostgreSQL instance):
cargo run compile examples/employees/average-title-salary.prql | psql -U postgres -d employees
- for each employee, find their average salary,
- join employees with their departments and titles (duplicating employees for each of their titles and departments)
- group by department and title, aggregating average salary
- join with department to get department name
PRQL
from salaries
group [emp_no] (
aggregate [emp_salary = average salary]
)
join t=titles [==emp_no]
join dept_emp side:left [==emp_no]
group [dept_emp.dept_no, t.title] (
aggregate [avg_salary = average emp_salary]
)
join departments [==dept_no]
select [dept_name, title, avg_salary]
SQL
from e=employees
join salaries [==emp_no]
group [e.emp_no, e.gender] (
aggregate [
emp_salary = average salaries.salary
]
)
join de=dept_emp [==emp_no] side:left
group [de.dept_no, gender] (
aggregate [
salary_avg = average emp_salary,
]
)
join departments [==dept_no]
select [dept_name, gender, salary_avg, salary_sd]
WITH table_3 AS (
SELECT
e.gender,
AVG(salaries.salary) AS _expr_0,
e.emp_no
FROM
JOIN salaries ON e.emp_no = salaries.emp_no
GROUP BY
e.emp_no,
e.gender
),
table_1 AS (
SELECT
table_2.gender,
AVG(table_2._expr_0) AS salary_avg,
STDDEV(table_2._expr_0) AS salary_sd,
de.dept_no
FROM
table_3 AS table_2
LEFT JOIN dept_emp AS de ON table_2.emp_no = de.emp_no
GROUP BY
de.dept_no,
table_2.gender
)
SELECT
departments.dept_name,
table_0.gender,
table_0.salary_avg,
table_0.salary_sd
FROM
table_1 AS table_0
JOIN departments ON table_0.dept_no = departments.dept_no
PRQL
SQL
WITH table_3 AS (
SELECT
e.gender,
e.emp_no
FROM
employees AS e
JOIN salaries ON e.emp_no = salaries.emp_no
e.emp_no,
e.gender
),
table_1 AS (
SELECT
AVG(table_2._expr_0) AS salary_avg,
STDDEV(table_2._expr_0) AS salary_sd,
dm.emp_no
FROM
table_3 AS table_2
JOIN dept_emp AS de ON table_2.emp_no = de.emp_no
JOIN dept_manager AS dm ON dm.dept_no = de.dept_no
AND (de.from_date, de.to_date) OVERLAPS (dm.from_date, dm.to_date)
GROUP BY
dm.emp_no,
table_2.gender
)
SELECT
managers.first_name || ' ' || managers.last_name AS mng_name,
managers.gender,
table_0.salary_avg,
table_0.salary_sd
FROM
table_1 AS table_0
JOIN employees AS managers ON table_0.emp_no = managers.emp_no
from de=dept_emp
join s=salaries side:left [
(s.emp_no == de.emp_no),
s"({s.from_date}, {s.to_date}) OVERLAPS ({de.from_date}, {de.to_date})"
]
group [de.emp_no, de.dept_no] (
aggregate salary = (average s.salary)
)
join employees [==emp_no]