Created
January 17, 2014 17:15
-
-
Save pedroelsner/8477359 to your computer and use it in GitHub Desktop.
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
// Carrega a tabela | |
Table: | |
load * inline [ | |
ID, ID2, UF, CABELO, OLHOS | |
1, 101, 'SP', 'PRETO', 'VERDE' | |
2, 102, 'RJ', 'CASTANHO', 'CASTANHO' | |
3, 103, 'SP', 'CASTANHO', 'AZUL' | |
4, 101, null, 'LOIRO', 'VERDE' | |
5, 104, 'RO', 'PRETO', 'VERDE' | |
6, 101, 'SP', 'PRETO', 'VERDE' | |
7, 105, 'SP', 'BRANCO', 'CASTANHO' | |
8, 101, 'SP', 'BRANCO', 'VERDE']; | |
// Conta o número de registros | |
T_Table: | |
LOAD Count(ID) AS CountID | |
Resident Table; | |
// Cria variavel | |
LET varCountID = peek('CountID' ,0,'T_Table'); | |
// Apaga tabela temporaria | |
DROP Table T_Table; | |
// Inicia loop | |
FOR i = 0 to $(varCountID) - 1 | |
// Pega o ID | |
LET varID = Peek('ID', $(i), 'Table'); | |
LET varUF = Peek('UF', $(i), 'Table'); | |
LET varCABELO = Peek('CABELO', $(i), 'Table'); | |
LET varOLHOS = Peek('OLHOS', $(i), 'Table'); | |
// Cria tabela com o ID | |
T_Table: | |
LOAD ID | |
Resident Table | |
where ID = $(varID); | |
// Regra para UF + CABELO | |
Left Join (T_Table) | |
Load $(varID) as ID, | |
FirstSortedValue(ID, -ID) as ID_CABELO | |
Resident Table | |
WHERE ID < $(varID) | |
and UF = '$(varUF)' | |
and CABELO = '$(varCABELO)' | |
GROUP BY $(varID); | |
// Regra para UF + OLHOS | |
Left Join (T_Table) | |
Load $(varID) as ID, | |
FirstSortedValue(ID, -ID) as ID_OLHOS | |
Resident Table | |
WHERE ID < $(varID) | |
and UF = '$(varUF)' | |
and OLHOS = '$(varOLHOS)' | |
GROUP BY $(varID); | |
// Gera tabela de resultado | |
ResultTable: | |
LOAD * | |
, '_hack' as hack // * hack para forçar criar a tabela | |
Resident T_Table; | |
// Apaga tabela temporaria | |
DROP Table T_Table; | |
NEXT | |
// Coloca resultado em Table | |
Left Join (Table) | |
Load ID, | |
if(ID_CABELO > ID_OLHOS, ID_CABELO, ID_OLHOS) as ULTIMOID | |
Resident ResultTable; | |
// Apaga tabela resultado | |
DROP Table ResultTable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment