0

Tengo una consulta en oracle:

WITH hist_snaps
            AS (SELECT instance_number,
            snap_id,
            round(begin_interval_time,'MI') datetime,
            (  begin_interval_time + 0 - LAG (begin_interval_time + 0)
            OVER (PARTITION BY dbid, instance_number ORDER BY snap_id)) * 86400 diff_time
            FROM dba_hist_snapshot), hist_stats
            AS (SELECT dbid,
            instance_number,
            snap_id,
            stat_name,
            VALUE - LAG (VALUE) OVER (PARTITION BY dbid,instance_number,stat_name ORDER BY snap_id)
            delta_value
            FROM dba_hist_sysstat
            WHERE stat_name IN ('user commits', 'user rollbacks'))
            SELECT datetime,
            ROUND (SUM (delta_value) / 3600, 2) as transactions
            FROM hist_snaps sn, hist_stats st, DUAL
            WHERE     st.instance_number = sn.instance_number
            AND st.snap_id = sn.snap_id
            AND datetime >= CURRENT_DATE - 1
            AND diff_time IS NOT NULL
            GROUP BY datetime
            ORDER BY 1

Esto me retorna 2 columnas asi:

introducir la descripción de la imagen aquí

Estoy tratando de hacer un WHEN para que cuando el dato que esta en transactions sea < 0 me coloque 0 en esa celda.

Trate con este codigo pero me da error:

WITH hist_snaps
            AS (SELECT instance_number,
            snap_id,
            round(begin_interval_time,'MI') datetime,
            (  begin_interval_time + 0 - LAG (begin_interval_time + 0)
            OVER (PARTITION BY dbid, instance_number ORDER BY snap_id)) * 86400 diff_time
            FROM dba_hist_snapshot), hist_stats
            AS (SELECT dbid,
            instance_number,
            snap_id,
            stat_name,
            VALUE - LAG (VALUE) OVER (PARTITION BY dbid,instance_number,stat_name ORDER BY snap_id)
            delta_value
            FROM dba_hist_sysstat
            WHERE stat_name IN ('user commits', 'user rollbacks'))
            SELECT datetime,
            CASE ROUND (SUM (delta_value) / 3600, 2) as transactions1
                WHEN transactions1 < 0 THEN 0
            END AS transactions
            FROM hist_snaps sn, hist_stats st, DUAL
            WHERE     st.instance_number = sn.instance_number
            AND st.snap_id = sn.snap_id
            AND datetime >= CURRENT_DATE - 1
            AND diff_time IS NOT NULL
            GROUP BY datetime
            ORDER BY 1

1 respuesta 1

0

Creo que no se puede utilizar el resultado del alias en la misma consulta, intenta esto:

WITH hist_snaps
            AS (SELECT instance_number,
            snap_id,
            round(begin_interval_time,'MI') datetime,
            (  begin_interval_time + 0 - LAG (begin_interval_time + 0)
            OVER (PARTITION BY dbid, instance_number ORDER BY snap_id)) * 86400 diff_time
            FROM dba_hist_snapshot), hist_stats
            AS (SELECT dbid,
            instance_number,
            snap_id,
            stat_name,
            VALUE - LAG (VALUE) OVER (PARTITION BY dbid,instance_number,stat_name ORDER BY snap_id)
            delta_value
            FROM dba_hist_sysstat
            WHERE stat_name IN ('user commits', 'user rollbacks'))
            SELECT datetime,
            CASE 
            WHEN ROUND (SUM (delta_value) / 3600, 2) < 0 THEN 0
            END AS transactions
            FROM hist_snaps sn, hist_stats st, DUAL
            WHERE     st.instance_number = sn.instance_number
            AND st.snap_id = sn.snap_id
            AND datetime >= CURRENT_DATE - 1
            AND diff_time IS NOT NULL
            GROUP BY datetime
            ORDER BY 1

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.