Add a 'Virtual' column to a SELECT in Oracle

Asked

Viewed 956 times

1

I would like to place a temporary column that was generated only in the query that will be linked to CD_NIVEL say, "If CD_NIVEL = 5 creates a column 'Exclui' as if it were a description of what the 5 means, I searched here and I found nothing I am new and I am learning SQL.

SQL code:

   select  a.cd_usuario
,       (select nm_usuario from adm_usuario where cd_usuario=a.cd_usuario and cd_empresa=a.cd_empresa) as ds_usuario 
,       (select cd_funcionario from adm_usuario where cd_usuario=a.cd_usuario and cd_empresa=a.cd_empresa) as ds_funcionario
,       a.cd_empresa
,       a.cd_componente
,       a.cd_nivel
from ADM_USUCMPEMP a
where cd_nivel=5 
and (select tp_privilegio from adm_usuario where cd_usuario=a.cd_usuario and cd_empresa=a.cd_empresa)='1' 
and (select tp_bloqueio from adm_usuario where cd_usuario=a.cd_usuario and cd_empresa=a.cd_empresa)=0 
--and a.cd_usuario=6029 
and a.cd_empresa=90 
--and a.cd_componente like '%FISR031%'
order by 5

Resultado do SELECT

1 answer

3

You can use the CASE to achieve this in your select. Example on this link.

SELECT 
    a.cd_usuario,
    ...
    a.cd_nivel, 
    CASE
        WHEN a.cd_nivel = 5
        THEN 'Exclui'
    END AS DescNivel
FROM
    ...

Remembering that the other values will have value null in that column.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.