Skip to content
Advertisement

Oracle SQL: Case statement

I am not sure where to put the case statement whether in select or where clause. I am trying to get data from table c when cnt_subj = MEPC_PL then metric_id will be equal to 6135 from table m. Can anyone help? Thank you in advance! 🙂

SELECT DISTINCT
    m.metric_id         AS metric_id,
    m.subject_abbrevn   AS p_subj,
    m.table_abbrevn     AS cnt_subj,
    c.p_id              AS p_id,
    c.cnt_id            AS cnt_id,
    c.gl_id             AS gl_id,
    case c.cnt_subj
    when 'MEPC_PL' then 6135
    end m.metric_id
FROM
    T_CROMS_CAT_T   m,
    croms_qc_currency_1_v c

Advertisement

Answer

I think this is the query you are looking for:

SELECT DISTINCT
    m.metric_id         AS metric_id,
    m.subject_abbrevn   AS p_subj,
    m.table_abbrevn     AS cnt_subj,
    c.p_id              AS p_id,
    c.cnt_id            AS cnt_id,
    c.gl_id             AS gl_id,
FROM croms_qc_currency_1_v c
INNER JOIN T_CROMS_CAT_T m ON DECODE(c.cnt_subj, 'MEPC_PL', 6135, NULL) = m.metric_id;

Do you actually need the DISTINCT? Is there more than one row with those IDs? Also, you can expand this if you have other CNT_SUBJ values which you need to translate.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement