-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathv_checa_saldo_cliente.sql
41 lines (40 loc) · 976 Bytes
/
v_checa_saldo_cliente.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
CREATE VIEW v_checa_saldo_cliente AS
SELECT
c.id,
c.nome,
c.limite,
s.valor AS saldo_atual,
(COALESCE(valor_credito, 0) - COALESCE(valor_debito, 0)) AS saldo_calculado,
s.valor - (COALESCE(valor_credito, 0) - COALESCE(valor_debito, 0)) AS dif_saldo,
COALESCE(tot_c, 0) AS tot_transacao_c,
COALESCE(tot_d, 0) AS tot_transacao_d
FROM
public.cliente c
JOIN
public.saldocliente s ON c.id = s.cliente_id
LEFT JOIN (
SELECT
cliente_id,
COUNT(1) AS tot_c,
SUM(valor) AS valor_credito
FROM
public.transacao
WHERE
tipo = 'c'
GROUP BY
cliente_id
) AS total_credito ON c.id = total_credito.cliente_id
LEFT JOIN (
SELECT
cliente_id,
COUNT(1) AS tot_d,
SUM(valor) AS valor_debito
FROM
public.transacao
WHERE
tipo = 'd'
GROUP BY
cliente_id
) AS total_debito ON c.id = total_debito.cliente_id
ORDER BY
c.id;