NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

PASO 1 :
CREATE OR REPLACE TABLE catalog_lhcl_prod_bcp.bcp_edv_expclie_001.tmp_interacciones_relevantes AS
SELECT DISTINCT
hi.codclavepartycli,
hi.fecinteraccion
FROM catalog_lhcl_prod_bcp.bcp_udv_int_vu.h_interaccionagente hi
JOIN (
SELECT DISTINCT codclavepartycli, fecha_encuesta
FROM catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_v2
) base
ON hi.codclavepartycli = base.codclavepartycli
AND hi.fecinteraccion BETWEEN DATE_SUB(base.fecha_encuesta, 180) AND DATE_ADD(base.fecha_encuesta, 90);
--------------------------------------------------------------------------------------------------------------
PASO 2:
CREATE OR REPLACE TABLE catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_var_comportamiento AS
WITH base AS (
SELECT
ie.CIC,
ie.codclavepartycli,
ie.fecha_encuesta
FROM catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_v2 ie
),
interacciones AS (
SELECT
codclavepartycli,
fecinteraccion
FROM catalog_lhcl_prod_bcp.bcp_edv_expclie_001.tmp_interacciones_relevantes
),
agregados AS (
SELECT
b.CIC,
b.codclavepartycli,
b.fecha_encuesta,

-- Frecuencia últimos 3 meses
COUNT(CASE
WHEN i.fecinteraccion BETWEEN DATE_SUB(b.fecha_encuesta, 90) AND DATE_SUB(b.fecha_encuesta, 1)
THEN 1 END) AS frecuencia_agente_ult_3m,

-- Frecuencia entre 90 y 180 días antes
COUNT(CASE
WHEN i.fecinteraccion BETWEEN DATE_SUB(b.fecha_encuesta, 180) AND DATE_SUB(b.fecha_encuesta, 91)
THEN 1 END) AS frecuencia_prom_anterior,

-- Fecha de última visita previa
MAX(CASE
WHEN i.fecinteraccion < b.fecha_encuesta
THEN i.fecinteraccion END) AS ultima_visita,

-- Interacciones posteriores a encuesta (para definir fuga)
COUNT(CASE
WHEN i.fecinteraccion > b.fecha_encuesta AND i.fecinteraccion <= DATE_ADD(b.fecha_encuesta, 90)
THEN 1 END) AS interacciones_post_encuesta

FROM base b
LEFT JOIN interacciones i
ON b.codclavepartycli = i.codclavepartycli
GROUP BY b.CIC, b.codclavepartycli, b.fecha_encuesta
)

SELECT
*,

-- Variación porcentual entre periodos
CASE
WHEN frecuencia_prom_anterior = 0 THEN 0
ELSE (frecuencia_agente_ult_3m - frecuencia_prom_anterior) * 100.0 / frecuencia_prom_anterior
END AS variacion_frecuencia,

-- Días desde última visita
DATEDIFF(fecha_encuesta, ultima_visita) AS dias_desde_ultima_visita,

-- Flag si el uso bajó más de 30%
CASE
WHEN frecuencia_prom_anterior = 0 THEN 0
WHEN frecuencia_agente_ult_3m < 0.7 * frecuencia_prom_anterior THEN 1
ELSE 0
END AS flag_disminucion_uso,

-- Fuga si no regresó en 90 días tras encuesta
CASE
WHEN interacciones_post_encuesta = 0 THEN 1
ELSE 0
END AS fuga_agentes

FROM agregados;

SELECT * FROM catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_var_comportamiento
-------------------------------------------------------------------------------------
PASO 3 (nro_reclamos_ult_3_meses) :

CREATE OR REPLACE TABLE catalog_lhcl_prod_bcp.bcp_edv_expclie_001.tmp_reclamos_ult_3m AS
SELECT
ie.codclavepartycli,
ie.fecha_encuesta,
COUNT(r.codreclamo) AS nro_reclamos_ult_3_meses
FROM catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_var_comportamiento ie
LEFT JOIN catalog_lhcl_prod_bcp.bcp_udv_int_vu.h_reclamo r
ON r.codclavepartycli = ie.codclavepartycli
AND r.fecreclamo BETWEEN DATE_SUB(ie.fecha_encuesta, 90) AND DATE_SUB(ie.fecha_encuesta, 1)
GROUP BY ie.codclavepartycli, ie.fecha_encuesta;

CREATE OR REPLACE TABLE catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_var_comportamiento_reclamos AS
SELECT
base.*, -- Todo lo que ya habías generado antes
COALESCE(r.nro_reclamos_ult_3_meses, 0) AS nro_reclamos_ult_3_meses -- Reclamos (0 si no hay)
FROM catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_var_comportamiento base
LEFT JOIN catalog_lhcl_prod_bcp.bcp_edv_expclie_001.tmp_reclamos_ult_3m r
ON base.codclavepartycli = r.codclavepartycli
AND base.fecha_encuesta = r.fecha_encuesta;

SELECT * FROM catalog_lhcl_prod_bcp.bcp_edv_expclie_001.Interaccion_encuestas_var_comportamiento_reclamos
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.