SubConsulta que devuelva el regisro anterior
Publicado por José Santos (1 intervención) el 11/11/2021 20:07:04
Oracle database 11g
Buenas tardes,
Estoy tratando de escribir un query que en una columna me muestre el dato anterior al actual, me explico, en este caso son tickets de averias a un servicio telefonico.
Cada numero telefonico puede tener varios tickets de averias en fechas diferentes, para cada ticket quiero poder visualizar el anterior, sin embargo la subconsulta me arroja el primer ticket que tuvo ese numero, se que no está correcta ya que no soy experto y por esto solicito su ayuda.
SELECT
t1.id_averia, t1.fecha_cierre,
(select t2.id_averia from dwpags.averias_cerradas t2
where t1.numero_servicio = t2.numero_servicio
and t1.id_averia <> t2.id_averia
and t1.nivel_solucion = t2.nivel_solucion
and t2.fecha_cierre = (select max (t3.fecha_cierre) from averias_cerradas t3 where
t2.numero_servicio = t3.numero_servicio)) ticket_anterior
FROM
averias_cerradas t1
where
t1.id_compania <> 2
and t1.nivel_solucion = 3
and t1.numero_servicio = 8094122381
and t1.id_grado in ('WIRELINE','INTERNET','ESP_LINE')
Resultado:
id_averia------------ fecha_cierre------------ticket_anterior
10141924-----------2/28/2020 --------------- null
11527136-----------6/23/2021 --------------- 10141924
11826611-----------10/14/2021---------------10141924
Deberia quedar:
id_averia------------ fecha_cierre------------ticket_anterior
10141924-----------2/28/2020 --------------- null
11527136-----------6/23/2021 --------------- 10141924
11826611-----------10/14/2021---------------11527136
Buenas tardes,
Estoy tratando de escribir un query que en una columna me muestre el dato anterior al actual, me explico, en este caso son tickets de averias a un servicio telefonico.
Cada numero telefonico puede tener varios tickets de averias en fechas diferentes, para cada ticket quiero poder visualizar el anterior, sin embargo la subconsulta me arroja el primer ticket que tuvo ese numero, se que no está correcta ya que no soy experto y por esto solicito su ayuda.
SELECT
t1.id_averia, t1.fecha_cierre,
(select t2.id_averia from dwpags.averias_cerradas t2
where t1.numero_servicio = t2.numero_servicio
and t1.id_averia <> t2.id_averia
and t1.nivel_solucion = t2.nivel_solucion
and t2.fecha_cierre = (select max (t3.fecha_cierre) from averias_cerradas t3 where
t2.numero_servicio = t3.numero_servicio)) ticket_anterior
FROM
averias_cerradas t1
where
t1.id_compania <> 2
and t1.nivel_solucion = 3
and t1.numero_servicio = 8094122381
and t1.id_grado in ('WIRELINE','INTERNET','ESP_LINE')
Resultado:
id_averia------------ fecha_cierre------------ticket_anterior
10141924-----------2/28/2020 --------------- null
11527136-----------6/23/2021 --------------- 10141924
11826611-----------10/14/2021---------------10141924
Deberia quedar:
id_averia------------ fecha_cierre------------ticket_anterior
10141924-----------2/28/2020 --------------- null
11527136-----------6/23/2021 --------------- 10141924
11826611-----------10/14/2021---------------11527136
Valora esta pregunta


0