-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagamento_empregados.sql
47 lines (45 loc) · 1.19 KB
/
pagamento_empregados.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
WITH total_descontos_table AS (
SELECT
e.matr,
COALESCE(SUM(d.valor),
0) AS total_descontos
FROM
empregado AS e
LEFT JOIN emp_desc AS ed ON e.matr = ed.matr
LEFT JOIN desconto AS d ON ed.cod_desc = d.cod_desc
GROUP BY
e.matr
),
total_salario_table AS (
SELECT
emp.matr,
COALESCE(SUM(v.valor),
0) AS salario
FROM
empregado AS emp
LEFT JOIN emp_venc AS ev ON emp.matr = ev.matr
LEFT JOIN vencimento AS v ON ev.cod_venc = v.cod_venc
GROUP BY
emp.matr
)
SELECT
depart.nome AS "Departamento",
emp.nome AS "Empregado",
table_salarios.salario AS "Salario Bruto",
table_descontos.total_descontos AS "Total Desconto",
table_salarios.salario - table_descontos.total_descontos AS "Salario Liquidoaws"
FROM
departamento AS depart
INNER JOIN divisao div ON depart.cod_dep = div.cod_dep
INNER JOIN empregado emp ON div.cod_divisao = emp.lotacao_div
LEFT JOIN total_salario_table AS table_salarios ON emp.matr = table_salarios.matr
LEFT JOIN total_descontos_table AS table_descontos ON emp.matr = table_descontos.matr
GROUP BY
depart.nome,
emp.nome,
table_salarios.salario,
table_descontos.total_descontos,
"Salario Liquidoaws"
ORDER BY
"Salario Liquidoaws" DESC,
emp.nome DESC;