Tengo que escribir un SQL donde tengo que escribir SQL para calcular el RunID primero que no se ve. dejame explicarte con un ejemplo
Ex:
RunID | RunDate | ErrorID ----- | ---------- | --------- 101 | 04/11/2017 | 1 101 | 04/11/2017 | 2 101 | 04/11/2017 | 3 102 | 04/22/2017 | 2 102 | 04/22/2017 | 3 103 | 04/26/2017 | 1 104 | 04/27/2017 | 3 105 | 04/28/2017 | 4En el ejemplo anterior, RunID 101 tiene errores 1,2,3. RunID 102 tiene 2,3. Durante la segunda ejecución, no se encuentra el ErrorID 1. Por lo tanto, RunID primero no visto aquí hasta ahora es 102 Pero ErrorID 1 se encuentra nuevamente en RunID 103 y finalmente ErrorID 1 no se encuentra en RunID 104. La consulta debe dar los RunID como 104 que primero no se encuentran.
He intentado usar algunas funciones de ventana como adelanto y retraso, pero no ayuda.
Estos son los resultados esperados:
Fecha no vista por primera vez para ErrorID: 2
RunID | RunDate | ErrorID ----- | ---------- | --------- 103 | 04/26/2017 | 2Porque nunca se vio ErrorID 2 (primera instancia de no visto) después de RunID-102
Fecha no vista por primera vez para ErrorID: 1
RunID | RunDate | ErrorID ----- | ---------- | --------- 104 | 04/27/2017 | 1ErrorID 1 nunca se vio después de RunID-104
Fecha no vista por primera vez para ErrorID: 3
RunID | RunDate | ErrorID ----- | ---------- | --------- 105 | 04/28/2017 | 3ErrorID 3 nunca se vio después de RunID-105
so=> with l as ( with m as ( select distinct max(runid) over(partition by errorid),errorid from so80 ) , a as ( select distinct runid,errorid from so80 ) select distinct min(a.runid) over (partition by m.errorid),m.errorid from m join a on m.max < a.runid ) select s.* from so80 s join l on l.min=s.runid and s.errorid=s.errorid ; runid | rundate | errorid -------+--------------+--------- 104 | 04/27/2017 | 3 103 | 04/26/2017 | 1 105 | 04/28/2017 | 4 (3 rows)--Get the last runDate when an errorID was seen with t1 as (select runId,runDate,errorID ,first_value(runDate) over(partition by errorID order by runDate desc rows between unbounded preceding and unbounded following) as last_seen from tablename ) --Get the next runDate based on the previous result ,t2 as (select runid,errorID,runDate ,(select min(runDate) from t1 t11 where t11.runDate>t1.last_seen) as date_first_not_seen from t1 ) --Join it to the original table to get the runID information from that runDate in the previous result (t2) select distinct t.runid,t2.errorid,t.rundate from t2 join tablename t on t.rundate=t2.date_first_not_seeno
with t1 as (select runId,runDate,errorID ,first_value(runDate) over(partition by errorID order by runDate desc rows between unbounded preceding and unbounded following) as last_seen from tablename) select distinct t1.errorid ,first_value(t.runDate) over(partition by t1.errorID order by t1.runDate desc rows between unbounded preceding and unbounded following) as rundate ,first_value(t.runID) over(partition by t1.errorID order by t1.runDate desc rows between unbounded preceding and unbounded following) as runid from t1 join tablename t on t.runDate>t1.last_seen