Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielle-carv committed Jul 24, 2023
1 parent 9985f40 commit f4c0195
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
20 changes: 17 additions & 3 deletions models/br_mercadolivre_ofertas/item.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ TIME(
ELSE vendedor
END vendedor,
titulo,
categorias,
CASE
WHEN categorias = '[]' THEN null
WHEN TRIM(JSON_EXTRACT_ARRAY(categorias)[OFFSET(1)], '"') = '...' THEN
TRIM(JSON_EXTRACT_ARRAY(categorias)[OFFSET(2)], '"')
WHEN TRIM(JSON_EXTRACT_ARRAY(categorias)[OFFSET(0)], '"') = '...' THEN
TRIM(JSON_EXTRACT_ARRAY(categorias)[OFFSET(1)], '"')
ELSE TRIM(JSON_EXTRACT_ARRAY(categorias)[OFFSET(0)], '"')
END as categoria_principal,
CASE
WHEN categorias = '[]' THEN null
when categorias = '[]' then null
WHEN TRIM(JSON_EXTRACT_ARRAY(categorias)[OFFSET(1)], '"') = '...' THEN
ARRAY_TO_STRING(ARRAY(SELECT x FROM UNNEST(JSON_EXTRACT_ARRAY(categorias)) AS x WITH OFFSET
WHERE OFFSET > 3), ', ')
WHEN TRIM(JSON_EXTRACT_ARRAY(categorias)[OFFSET(0)], '"') = '...' THEN
ARRAY_TO_STRING(ARRAY(SELECT x FROM UNNEST(JSON_EXTRACT_ARRAY(categorias)) AS x WITH OFFSET
WHERE OFFSET > 1), ', ')
ELSE ARRAY_TO_STRING(ARRAY(SELECT x FROM UNNEST(JSON_EXTRACT_ARRAY(categorias)) AS x WITH OFFSET
WHERE OFFSET > 0), ', ')
END as outras_categorias,
Expand All @@ -50,7 +61,10 @@ TIME(
ELSE preco
END AS FLOAT64) AS preco_final,
FROM
`basedosdados-staging.br_mercadolivre_ofertas_staging.item`)
`basedosdados-staging.br_mercadolivre_ofertas_staging.item`

)

SELECT
data_consulta,
hora_consulta,
Expand All @@ -59,7 +73,7 @@ SELECT
titulo,
id_vendor as id_vendedor,
vendedor,
categoria_principal,
a.categoria_principal,
REGEXP_REPLACE(
TRIM(outras_categorias, '"'),
r'("([^"]+)")',
Expand Down
2 changes: 1 addition & 1 deletion models/br_mercadolivre_ofertas/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ models:
- name: categoria_principal
description: Primeira categoria do item classificada no site
- name: outras_categorias
description: Todas as categorias descritas no site em relação ao item
description: Demais categorias descritas no site em relação ao item
- name: caracteristicas
description: Características adicionais do item
- name: envio_nacional
Expand Down
57 changes: 54 additions & 3 deletions models/br_mercadolivre_ofertas/vendedor.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ WITH main AS (
from `basedosdados-staging.br_mercadolivre_ofertas_staging.vendedor`
), tabela_ordenada AS (
SELECT
PARSE_DATE('%Y-%m-%d', dia) AS data_consulta,
dia AS data_consulta,
id_municipio,
main.id_vendedor,
nome AS vendedor,
Expand All @@ -42,7 +42,58 @@ SELECT
SAFE_CAST(predata.opinioes.regular AS INT64) AS avaliacao_ruim
FROM main
LEFT JOIN predata
ON main.id_vendedor = predata.id_vendedor)
ON main.id_vendedor = predata.id_vendedor),

SELECT * FROM tabela_ordenada
tabela_deduplicada AS (
SELECT
PARSE_DATE('%Y-%m-%d', data_consulta) AS data_consulta,
id_municipio,
id_vendedor,
vendedor,
classificacao,
reputacao,
anos_experiencia,
ARRAY_AGG(avaliacao_bom)[OFFSET(0)] AS avaliacao_bom,
ARRAY_AGG(avaliacao_regular)[OFFSET(0)] AS avaliacao_regular,
ARRAY_AGG(avaliacao_ruim)[OFFSET(0)] AS avaliacao_ruim
FROM
tabela_ordenada
GROUP BY
data_consulta,
id_vendedor,
vendedor,
anos_experiencia,
reputacao,
classificacao,
id_municipio
HAVING
COUNT(*) > 1
), tabela_unicos AS (
SELECT
PARSE_DATE('%Y-%m-%d', data_consulta) AS data_consulta,
id_municipio,
id_vendedor,
vendedor,
classificacao,
reputacao,
anos_experiencia,
ARRAY_AGG(avaliacao_bom)[OFFSET(0)] AS avaliacao_bom,
ARRAY_AGG(avaliacao_regular)[OFFSET(0)] AS avaliacao_regular,
ARRAY_AGG(avaliacao_ruim)[OFFSET(0)] AS avaliacao_ruim
FROM
tabela_ordenada
GROUP BY
data_consulta,
id_vendedor,
vendedor,
anos_experiencia,
reputacao,
classificacao,
id_municipio
HAVING
COUNT(*) = 1
)
SELECT * FROM tabela_unicos
UNION ALL
SELECT * FROM tabela_deduplicada

0 comments on commit f4c0195

Please sign in to comment.