Skip to content

Instantly share code, notes, and snippets.

@CarMoreno
Last active November 19, 2020 15:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save CarMoreno/9331ee4f74071bdfeaea06e7d4dc510e to your computer and use it in GitHub Desktop.
Unir tablas PIVOT eliminando duplicados - Bigquery
CREATE OR REPLACE TABLE `tabla_c` AS
(
WITH tabla_intermedia AS (
SELECT * FROM `tabla_a` #Tabla Inicial
UNION DISTINCT
SELECT * FROM `tabla_2` #Tabla particionada 2
)
SELECT row[OFFSET(0)].* FROM (
SELECT ARRAY_AGG(t ORDER BY fecha_consulta DESC LIMIT 1) row
FROM tabla_intermedia t
GROUP BY id_interno
)ORDER BY id_interno
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment