\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Oficina de Datos (Miguel Ortiz)} \pdfinfo{ /Title (buenas-practicas-sql.pdf) /Creator (Cheatography) /Author (Oficina de Datos (Miguel Ortiz)) /Subject (Buenas practicas SQL Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{ED2D2D} \definecolor{LightBackground}{HTML}{FDF1F1} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Buenas practicas SQL Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Oficina de Datos (Miguel Ortiz)} via \textcolor{DarkBackground}{\uline{cheatography.com/148304/cs/32343/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Oficina de Datos (Miguel Ortiz) \\ \uline{cheatography.com/miguel-ortiz} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 6th June, 2022.\\ Updated 23rd September, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Creación de tablas}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{¡Importante!}} Es obligatorio guardar las tablas como Parquet y no en formato Text File en el lago de datos debido a que estos últimos ocupan más espacio de almacenamiento.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Estructura para crear esto desde Hive}}} \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{CREATE TABLE Nombre\_Tabla(Schema) ROW FORMAT DELIMITED FIELDS TERMINATED BY ";" STORED AS PARQUET;} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Sentencia SELECT}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Evitar usar {\bf{SELECT }}*. Es preferible listar las columnas exactas que se usarán en el análisis de forma que se optimice el almacenamiento y procesamiento de la consulta.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{a. Código sin optimizar}}} \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{-{}- Tabla t1 tiene 90 columnas \{\{nl\}\} {\bf{select }}* \{\{nl\}\} from tabla1 t1} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado}}} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7 \{\{nl\}\} from tabla1 t1} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Por otro lado, use en lo posible, la función LIMIT para limitar el número de filas visualizadas.}}} \tn % Row Count 13 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Select}}* \{\{nl\}\} from tabla1 t1 \{\{nl\}\} Limit 10} \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Delimitación de periodos}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Evitar realizar consultas que no contengan un {\bf{periodo delimitado}}, esto permite que sea mas eficiente, optimizando el almacenamiento y procesamineto.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{a. Código sin optimizar.}}} \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Select}} \{\{nl\}\} t1.var1 \{\{nl\}\} t2.var2 \{\{nl\}\} {\bf{from}} tabla1 t1} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado.}}} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Select}} \{\{nl\}\} t1.var1 \{\{nl\}\} t2.var2 \{\{nl\}\} {\bf{from}} tabla1 t1 \{\{nl\}\} {\bf{where periodo = AAAAMMDD}}} \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{c. Código optimizado 2.}}} \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Select}} \{\{nl\}\} t1.var1 \{\{nl\}\} t2.var2 \{\{nl\}\} {\bf{from}} tabla1 t1 \{\{nl\}\} {\bf{where periodo Between AAAAMMDD and AAAAMMDD}}} \tn % Row Count 15 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Por otro lado, si no utiliza un delimitador de periodo debe usar la función LIMIT}} para limitar el número de filas visualizadas.} \tn % Row Count 18 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Select}} \{\{nl\}\} {\bf{from}} tabla1 t1 \{\{nl\}\} {\bf{Limit}} 10} \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Sentencia WHERE vs JOIN}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Evitar cruces entre tablas usando WHERE, preferir la definición de cruces con JOIN y todas sus variaciones (left, right, inner, etc)} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{a. Código sin optimizar}}} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7, \{\{nl\}\} t2.var2 \{\{nl\}\} {\bf{from}} tabla1 t1, tabla2 t2 \{\{nl\}\} {\bf{where}} t1.var1 = t2.var5} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado}}} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7, \{\{nl\}\} t2.var2 {\bf{from}} tabla1 t1 inner join tabla2 t2 \{\{nl\}\} ~ {\bf{on}} t1.var1 = t2.var5} \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Sentencia DISTINCT vs GROUP BY}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Para eliminar duplicados preferir {\bf{DISTINCT}} en vez de {\bf{GROUP BY.}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{a. Código sin optimizar}}} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} t1.var1 \{\{nl\}\} t1.var7 \{\{nl\}\} {\bf{from}} tabla1 t1 \{\{nl\}\} {\bf{group by}} t1.var1, t1.var7} \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado}}} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select distinct}} \{\{nl\}\} t1.var1 \{\{nl\}\} t1.var7 \{\{nl\}\}{\bf{from}} tabla1 t1} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Sentencias UPDATE vs CASE WHEN}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Evitar sentencias de {\bf{UPDATE}} posteriores a consultas y reemplazarlas en lo posible por sentencias de {\bf{CASE WHEN}} para rehusar las sentencias de códigos previas.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{a. Código sin optimizar}}} \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{-{}-{}- Consulta genera tabla: TemporalTabla \{\{nl\}\} {\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7, \{\{nl\}\} t2.var2 \{\{nl\}\}{\bf{from}} tabla1 t1 {\bf{left join}} tabla2 t2 \{\{nl\}\}~{\bf{on}} t1.var1 = t2.var5 \{\{nl\}\} -{}-{}- Actualización de tabla: TemporalTabla \{\{nl\}\} {\bf{update}} TemporalTabla \{\{nl\}\}{\bf{set}} var2 = "No aplica" \{\{nl\}\}{\bf{where}} var7 is null} \tn % Row Count 12 (+ 7) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado}}} \tn % Row Count 13 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}}\{\{nl\}\} t1.var1 \{\{nl\}\} t1.var7 \{\{nl\}\} {\bf{case}}\{\{nl\}\} {\bf{when}} var7 is null then "no aplica" \{\{nl\}\}{\bf{else}} t2.var2 \{\{nl\}\}{\bf{end as}} var2 \{\{nl\}\}{\bf{from}} tabla1 t1 {\bf{left join}} tabla2 t2 \{\{nl\}\}{\bf{on}} t1.var1 = t2.var5} \tn % Row Count 18 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Sentencia IN/NOT IN vs JOINS o WITH}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Evitar al máximo consultas anidadas y el uso de {\bf{IN/NOT IN.}} Reemplazar por {\bf{JOINS}} o por la cláusula {\bf{WITH}} para garantizar un código más fácil de leer y/o depurar.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{nl\}\}{\bf{a. Código sin optimizar 1}}} \tn % Row Count 5 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7 \{\{nl\}\}{\bf{from}} tabla1 t1 \{\{nl\}\} {\bf{where}} t1.var1 {\bf{in}} ({\bf{select}} t2.columna {\bf{from}} tabla2 t2)} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado 1}}} \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7 \{\{nl\}\}{\bf{from}} tabla1 t1 {\bf{inner join}} tabla2 t2 \{\{nl\}\}{\bf{on}} t1.var1 = t2.columna} \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{c. Código sin optimizar }}} \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7 \{\{nl\}\}{\bf{from}} tabla1 t1 {\bf{inner join}} \{\{nl\}\}( \{\{nl\}\}{\bf{select}} \{\{nl\}\}t2.columna \{\{nl\}\}{\bf{from}} tabla2 t2 {\bf{left join}}* tabla3 t3 \{\{nl\}\}{\bf{on}} t2.var1 = t3.var1 \{\{nl\}\}) temporal \{\{nl\}\} {\bf{on}} t1.var1 = temporal.columna} \tn % Row Count 19 (+ 6) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{d. Código optimizado 2}}} \tn % Row Count 20 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{with temporal as ( \{\{nl\}\}{\bf{select}} \{\{nl\}\} t2.columna \{\{nl\}\} {\bf{from}} tabla2 t2 {\bf{left join}} tabla3 t3 \{\{nl\}\}{\bf{on}} t2.var1 = t3.var1 \{\{nl\}\} ) \{\{nl\}\} {\bf{select}} \{\{nl\}\} t1.var1, \{\{nl\}\} t1.var7 \{\{nl\}\} {\bf{from}} tabla1 t1 {\bf{inner join}} temporal \{\{nl\}\}{\bf{on}} t1.var1 = temporal.columna} \tn % Row Count 26 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Unificación de procesos}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Unificar procesos similares. No repetir procesos.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{a. Código sin optimizar}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{update}} \{\{nl\}\} \seqsplit{temp\_comercios\_nuevos\_cr} \{\{nl\}\} {\bf{set}} \{\{nl\}\} nuevo\_nombre = {\bf{replace}}(nuevo\_nombre, 'www.','') \{\{nl\}\}{\bf{where}} \{\{nl\}\}nuevo\_nombre {\bf{like}} '\%www.\%' \{\{nl\}\} {\bf{update}} \{\{nl\}\} \seqsplit{temp\_comercios\_nuevos\_cr} \{\{nl\}\} {\bf{set}} \{\{nl\}\} nuevo\_nombre = {\bf{replace}}(nuevo\_nombre, '/bill','') {\bf{where}} \{\{nl\}\} nuevo\_nombre like '/bill' -{}-etc} \tn % Row Count 10 (+ 8) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado}}} \tn % Row Count 11 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} {\bf{case}} \{\{nl\}\} {\bf{when}} nuevo\_nombre {\bf{like}} '\%/bill\%' {\bf{then}} \{\{nl\}\} {\bf{replace}} (nuevo\_nombre, '/bill','') \{\{nl\}\}{\bf{when}} nuevo\_nombre {\bf{like}} '\%www.\%' {\bf{then}}\{\{nl\}\} {\bf{replace}}(nuevo\_nombre, 'www.','') \{\{nl\}\}{\bf{else}} nuevo\_nombre \{\{nl\}\}{\bf{end}} as nuevo\_nombre \{\{nl\}\}{\bf{from}} \seqsplit{temp\_comercios\_nuevos\_cr}} \tn % Row Count 18 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Convenciones}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Evitar el uso de alias o nombres temporales ambiguos. Se recomienda aclarar si la construcción es temporal y a que hace referencia, de forma que el proceso de negocio sea coherente y fácil de manipular.} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{a. Código sin optimizar}}} \tn % Row Count 6 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{select}} \{\{nl\}\} A.var1, \{\{nl\}\}A.var7 \{\{nl\}\}{\bf{from}} tabla1 A {\bf{inner join}}\{\{nl\}\} ( \{\{nl\}\} {\bf{select}} \{\{nl\}\}t2.columna \{\{nl\}\}{\bf{from}} tabla2 B {\bf{left join}} tabla3 C \{\{nl\}\}{\bf{on}} B.var1 = C.var1 \{\{nl\}\} )D \{\{nl\}\} {\bf{on}} A.var1 = D.columna} \tn % Row Count 11 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{b. Código optimizado}}} \tn % Row Count 12 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{with estado\_tx as ( \{\{nl\}\} {\bf{select}} t2. columna \{\{nl\}\} {\bf{from}} tabla2 movto {\bf{left join}} tabla3 novedades \{\{nl\}\} {\bf{on}} movto.var1 = novedades.var1 ) \{\{nl\}\} {\bf{select}} \{\{nl\}\} saldos.var1, \{\{nl\}\} saldos.var7 \{\{nl\}\} {\bf{from}} tabla1.saldos {\bf{inner join}} estado\_tx \{\{nl\}\} {\bf{on}} saldos.var1 = estado\_tx.columna} \tn % Row Count 19 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Nombramiento tablas temporales}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A manera de estándar se recomienda que las tablas temporales creadas dentro de un proceso de SQL sigan la siguiente estructura: {\bf{temp\_AbreviaturaProyecto\_nombreTabla.}}} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Nombres de variables y tablas}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Como se mencionó en el punto anterior, se debe evitar la ambigüedad en las tablas o subconsultas. Sin embargo, esto aplica también en el nombramiento de las columnas en las que se recomienda usar el separador "\_" para usar nombres largos. Usar abreviaciones cuando se requiera, sin que el uso de las mismas puedan confundir al lector del código. \{\{nl\}\} {\bf{(Ejemplo: Reemplazar NAprobaTjCredCl por nro\_aprobacion\_tdc\_cliente)}}} \tn % Row Count 9 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}