From 2a97f19af705feb2dd1ff64cc3f3344626bb0b7c Mon Sep 17 00:00:00 2001 From: Vitor Salazar Cicote <67997056+vtrsc@users.noreply.github.com> Date: Mon, 29 May 2023 11:51:49 -0300 Subject: [PATCH 1/5] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a6c60b2..e4ab7b1 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# pizza-query \ No newline at end of file + + +# SQL ESSENTIALS From c4f31b669b9b34f2744833ae9a24e172fc7f4aa5 Mon Sep 17 00:00:00 2001 From: Vitor Salazar Cicote <67997056+vtrsc@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:18:58 -0300 Subject: [PATCH 2/5] Create EXEMPLOS SQL --- EXEMPLOS SQL | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 EXEMPLOS SQL diff --git a/EXEMPLOS SQL b/EXEMPLOS SQL new file mode 100644 index 0000000..e7ada5d --- /dev/null +++ b/EXEMPLOS SQL @@ -0,0 +1,197 @@ +Exemplos de SQL +SQL Recursivo de CRs + +1 + +/*Exemplo 1*/ + +2 + + + +3 + +SELECT CONCAT(LPAD(' ',LEVEL*3-3),VW.CODUNIDADM || '.' || VW.NOMEUNIDADM) AS DESCRICAO + +4 + + ,DECODE(TIPOGRUPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA',TIPOGRUPO) AS TIPOGRUPO + +5 + + ,CODGRUPO AS CR_PAI + +6 + + ,CODUNIDADM AS CR + +7 + + ,LEVEL + +8 + + FROM (SELECT * + +9 + + FROM CR M + +10 + + WHERE TO_NUMBER(M.CODUNIDADM) NOT IN (40413,40823)) VW + +11 + +CONNECT BY PRIOR TO_NUMBER(VW.CODUNIDADM) = TO_NUMBER(VW.CODGRUPO) + +12 + + START WITH TO_NUMBER(VW.CODGRUPO) >= 0; + +13 + + + +14 + + + +15 + +/*Exemplo 2 - Com os Responsaveis*/ + +16 + + + +17 + +SELECT COD_CR_PAI AS CR_PAI + +18 + + ,COD_CR AS CR + +19 + + ,CONCAT(LPAD('.',LEVEL*5-5,'.'),VW.COD_CR || '.' || VW.DSC_CR) AS DESCRICAO + +20 + + ,DSC_CR_ABREVIACAO AS ABREVIACAO + +21 + + ,DSC_CR_SIGLA AS SIGLA + +22 + + ,DECODE(IND_TIPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA','P','PREFETURA MUNICIPAL',IND_TIPO) + +23 + + AS TIPO + +24 + + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 0) + +25 + + AS PREFEITO + +26 + + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 1) + +27 + + AS SECRETARIO + +28 + + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 2) + +29 + + AS DIRETOR + +30 + + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 3) + +31 + + AS ASSISTENTE + +32 + + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 4) + +33 + + AS GERENTE + +34 + + FROM (SELECT M.COD_CR + +35 + + ,M.COD_CR_PAI AS COD_CR_PAI + +36 + + ,M.DSC_CR + +37 + + ,M.DSC_CR_ABREVIACAO + +38 + + ,M.DSC_CR_SIGLA + +39 + + ,M.IND_TIPO + +40 + + ,M.COD_RESPONSAVEL_1 + +41 + + ,M.COD_RESPONSAVEL_2 + +42 + + FROM BDM_CR M + +43 + + WHERE M.COD_CR NOT IN (40413,40823) + +44 + + AND M.DTA_EXCLUSAO IS NULL) VW + +45 + + CONNECT BY PRIOR VW.COD_CR = TO_NUMBER(VW.COD_CR_PAI) + +46 + + START WITH TO_NUMBER(VW.COD_CR_PAI) >= 0 + +47 + + AND TO_NUMBER(VW.COD_CR) = 10000; + +48 + + + + +Resultados From 4b3d3806d431e7eb03bed409026691e029ee3b16 Mon Sep 17 00:00:00 2001 From: Vitor Salazar Cicote <67997056+vtrsc@users.noreply.github.com> Date: Fri, 22 Sep 2023 11:08:48 -0300 Subject: [PATCH 3/5] Update README.md --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index e4ab7b1..e14de0c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,51 @@ # SQL ESSENTIALS +/*Exemplo 1*/ + +SELECT CONCAT(LPAD(' ',LEVEL*3-3),VW.CODUNIDADM || '.' || VW.NOMEUNIDADM) AS DESCRICAO + ,DECODE(TIPOGRUPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA',TIPOGRUPO) AS TIPOGRUPO + ,CODGRUPO AS CR_PAI + ,CODUNIDADM AS CR + ,LEVEL + FROM (SELECT * + FROM CR M + WHERE TO_NUMBER(M.CODUNIDADM) NOT IN (40413,40823)) VW +CONNECT BY PRIOR TO_NUMBER(VW.CODUNIDADM) = TO_NUMBER(VW.CODGRUPO) + START WITH TO_NUMBER(VW.CODGRUPO) >= 0; + + +/*Exemplo 2 - Com os Responsaveis*/ + +SELECT COD_CR_PAI AS CR_PAI + ,COD_CR AS CR + ,CONCAT(LPAD('.',LEVEL*5-5,'.'),VW.COD_CR || '.' || VW.DSC_CR) AS DESCRICAO + ,DSC_CR_ABREVIACAO AS ABREVIACAO + ,DSC_CR_SIGLA AS SIGLA + ,DECODE(IND_TIPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA','P','PREFETURA MUNICIPAL',IND_TIPO) + AS TIPO + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 0) + AS PREFEITO + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 1) + AS SECRETARIO + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 2) + AS DIRETOR + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 3) + AS ASSISTENTE + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 4) + AS GERENTE + FROM (SELECT M.COD_CR + ,M.COD_CR_PAI AS COD_CR_PAI + ,M.DSC_CR + ,M.DSC_CR_ABREVIACAO + ,M.DSC_CR_SIGLA + ,M.IND_TIPO + ,M.COD_RESPONSAVEL_1 + ,M.COD_RESPONSAVEL_2 + FROM BDM_CR M + WHERE M.COD_CR NOT IN (40413,40823) + AND M.DTA_EXCLUSAO IS NULL) VW + CONNECT BY PRIOR VW.COD_CR = TO_NUMBER(VW.COD_CR_PAI) + START WITH TO_NUMBER(VW.COD_CR_PAI) >= 0 + AND TO_NUMBER(VW.COD_CR) = 10000; + From 1e8015b227e633ddf052c8cc9244e64bd6c9d717 Mon Sep 17 00:00:00 2001 From: Vitor Salazar Cicote <67997056+vtrsc@users.noreply.github.com> Date: Fri, 22 Sep 2023 11:09:58 -0300 Subject: [PATCH 4/5] Create Recursivo.sql --- dia02/Recursivo.sql | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 dia02/Recursivo.sql diff --git a/dia02/Recursivo.sql b/dia02/Recursivo.sql new file mode 100644 index 0000000..1eafa6b --- /dev/null +++ b/dia02/Recursivo.sql @@ -0,0 +1,48 @@ +/*Exemplo 1*/ + +SELECT CONCAT(LPAD(' ',LEVEL*3-3),VW.CODUNIDADM || '.' || VW.NOMEUNIDADM) AS DESCRICAO + ,DECODE(TIPOGRUPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA',TIPOGRUPO) AS TIPOGRUPO + ,CODGRUPO AS CR_PAI + ,CODUNIDADM AS CR + ,LEVEL + FROM (SELECT * + FROM CR M + WHERE TO_NUMBER(M.CODUNIDADM) NOT IN (40413,40823)) VW +CONNECT BY PRIOR TO_NUMBER(VW.CODUNIDADM) = TO_NUMBER(VW.CODGRUPO) + START WITH TO_NUMBER(VW.CODGRUPO) >= 0; + + +/*Exemplo 2 - Com os Responsaveis*/ + +SELECT COD_CR_PAI AS CR_PAI + ,COD_CR AS CR + ,CONCAT(LPAD('.',LEVEL*5-5,'.'),VW.COD_CR || '.' || VW.DSC_CR) AS DESCRICAO + ,DSC_CR_ABREVIACAO AS ABREVIACAO + ,DSC_CR_SIGLA AS SIGLA + ,DECODE(IND_TIPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA','P','PREFETURA MUNICIPAL',IND_TIPO) + AS TIPO + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 0) + AS PREFEITO + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 1) + AS SECRETARIO + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 2) + AS DIRETOR + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 3) + AS ASSISTENTE + ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 4) + AS GERENTE + FROM (SELECT M.COD_CR + ,M.COD_CR_PAI AS COD_CR_PAI + ,M.DSC_CR + ,M.DSC_CR_ABREVIACAO + ,M.DSC_CR_SIGLA + ,M.IND_TIPO + ,M.COD_RESPONSAVEL_1 + ,M.COD_RESPONSAVEL_2 + FROM BDM_CR M + WHERE M.COD_CR NOT IN (40413,40823) + AND M.DTA_EXCLUSAO IS NULL) VW + CONNECT BY PRIOR VW.COD_CR = TO_NUMBER(VW.COD_CR_PAI) + START WITH TO_NUMBER(VW.COD_CR_PAI) >= 0 + AND TO_NUMBER(VW.COD_CR) = 10000; + From 32e6e6da397584e41bfcee535b0b0cbb544a2ffe Mon Sep 17 00:00:00 2001 From: Vitor Salazar Cicote <67997056+vtrsc@users.noreply.github.com> Date: Fri, 22 Sep 2023 11:10:34 -0300 Subject: [PATCH 5/5] Update README.md --- README.md | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/README.md b/README.md index e14de0c..36762ba 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,4 @@ -# SQL ESSENTIALS -/*Exemplo 1*/ -SELECT CONCAT(LPAD(' ',LEVEL*3-3),VW.CODUNIDADM || '.' || VW.NOMEUNIDADM) AS DESCRICAO - ,DECODE(TIPOGRUPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA',TIPOGRUPO) AS TIPOGRUPO - ,CODGRUPO AS CR_PAI - ,CODUNIDADM AS CR - ,LEVEL - FROM (SELECT * - FROM CR M - WHERE TO_NUMBER(M.CODUNIDADM) NOT IN (40413,40823)) VW -CONNECT BY PRIOR TO_NUMBER(VW.CODUNIDADM) = TO_NUMBER(VW.CODGRUPO) - START WITH TO_NUMBER(VW.CODGRUPO) >= 0; - - -/*Exemplo 2 - Com os Responsaveis*/ - -SELECT COD_CR_PAI AS CR_PAI - ,COD_CR AS CR - ,CONCAT(LPAD('.',LEVEL*5-5,'.'),VW.COD_CR || '.' || VW.DSC_CR) AS DESCRICAO - ,DSC_CR_ABREVIACAO AS ABREVIACAO - ,DSC_CR_SIGLA AS SIGLA - ,DECODE(IND_TIPO,'S','SECRETARIA','D','DEPARTAMENTO','G','GERENCIA','E','ENCARREGATURA','P','PREFETURA MUNICIPAL',IND_TIPO) - AS TIPO - ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 0) - AS PREFEITO - ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 1) - AS SECRETARIO - ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 2) - AS DIRETOR - ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 3) - AS ASSISTENTE - ,(SELECT RTRIM(XMLAGG(XMLELEMENT(E,C.DSC_NOME_SERVIDOR || chr(10))).EXTRACT('//text()'), chr(10)) FROM BDM_CR_RESPONSAVEIS C WHERE COD_LOTACAO = COD_CR AND NIVEL = 4) - AS GERENTE - FROM (SELECT M.COD_CR - ,M.COD_CR_PAI AS COD_CR_PAI - ,M.DSC_CR - ,M.DSC_CR_ABREVIACAO - ,M.DSC_CR_SIGLA - ,M.IND_TIPO - ,M.COD_RESPONSAVEL_1 - ,M.COD_RESPONSAVEL_2 - FROM BDM_CR M - WHERE M.COD_CR NOT IN (40413,40823) - AND M.DTA_EXCLUSAO IS NULL) VW - CONNECT BY PRIOR VW.COD_CR = TO_NUMBER(VW.COD_CR_PAI) - START WITH TO_NUMBER(VW.COD_CR_PAI) >= 0 - AND TO_NUMBER(VW.COD_CR) = 10000;