-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path17_cte.sql
More file actions
29 lines (22 loc) · 655 Bytes
/
17_cte.sql
File metadata and controls
29 lines (22 loc) · 655 Bytes
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
-- CTE: COMMON TABLE EXPRESSION
WITH tb_cliente_primeiro_dia AS (
SELECT DISTINCT IdCliente
FROM transacoes
WHERE substr(dtCriacao,1,10) = '2025-08-25'
),
tb_cliente_ultimo_dia AS (
SELECT DISTINCT IdCliente
FROM transacoes
WHERE substr(dtCriacao,1,10) = '2025-08-29'
),
tb_join AS (
SELECT t1.IdCliente AS primCliente,
t2.IdCliente AS ultCliente
FROM tb_cliente_primeiro_dia AS t1
LEFT JOIN tb_cliente_ultimo_dia AS t2
ON t1.IdCliente = t2.IdCliente
)
SELECT count(primCliente),
count(ultCliente),
1. * count(ultCliente) / count(primCliente)
FROM tb_join