Last active
May 16, 2019 00:31
-
-
Save jruizvar/2f484c00aaa98e97d8ff2cc9c757d60d to your computer and use it in GitHub Desktop.
Filtra empresas com historico completo, e calcula o sinal acumulado.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS temp1; | |
CREATE TABLE temp1 (anomes INT, cnpj STRING, x DOUBLE); | |
INSERT INTO temp1 VALUES | |
(201801, 'c0', 1), | |
(201801, 'c1', 1), | |
(201801, 'c2', 3), | |
(201802, 'c0', 2), | |
(201802, 'c1', 2), | |
(201802, 'c2', null), | |
(201803, 'c0', 2), | |
(201803, 'c1', 1), | |
(201803, 'c2', 2), | |
(201804, 'c0', null), | |
(201804, 'c1', 2), | |
(201804, 'c2', 5); | |
SELECT | |
cnpj, | |
anomes, | |
x, | |
sum(x) OVER w AS cumx | |
FROM ( | |
SELECT | |
cnpj, | |
anomes, | |
x, | |
count(x) OVER (PARTITION BY cnpj) AS countx | |
FROM temp1 | |
) t0 | |
WHERE countx = 4 -- filtra empresas com historico completo | |
WINDOW w AS | |
( | |
PARTITION BY cnpj | |
ORDER BY anomes | |
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment