Decode in full select

0

Can I perform a decode on a select that returns all the data in a table and some of the other tables?

In this select, I would like to do a decode for one of the columns of PP (request_peace), but as it does a full search I do not know if it is possible and if possible, I do not know how.

For example: decode (cd_group, 1, Freq, 2, Accessory, null) 'Column of the request_table table in which I want to perform decode.

SELECT pp.*,
       g.nu_ordem_servico,
       g.dt_entrada,
       (g.dt_entrada + 30) as dt_vencimento,
       (select w.nm_perfil2
          from workflow w
         where w.id_processo = 7
           and w.cd_laudo = pp.id_status) as ds_status,
       conc.nu_cnpj || ' - ' || conc.sg_concessionario || ' - ' ||
       conc.nm_concessionario || ' - ' || conc.nm_cidade || ' - ' ||
       conc.sg_uf as sg_nm_concessionario,
       (select count(1)
          from ivv
         where dt_exclusao is null
           and nu_chassi = pp.ds_custom01
           and dt_limite_garantia > pp.dt_pedido) as ic_tem_garantia
  FROM pedido_peca pp
  left join garantia g
    on pp.nu_sg = g.nu_sg, concessionario conc
 WHERE pp.dt_exclusao is null
   and conc.dt_exclusao is null
   and g.dt_exclusao is null
   and pp.cd_concessionario = conc.id_concessionario
   and pp.id_pedido = 50360
   and pp.cd_concessionario = 5 

Here is the code in VB.NET that will return that select above.

        cfg.Abrir(DBProvider.enConnectionType.PEC)

        'Obtem Pedido
        sb = New System.Text.StringBuilder
        sb.Append(" SELECT ")
        sb.Append(" pp.*, ")
        sb.Append(" g.nu_ordem_servico, ")
        sb.Append(" g.dt_entrada, ")
        sb.Append(" (g.dt_entrada + 30) as dt_vencimento, ")
        sb.Append(" (select w.nm_perfil" & idPerfil & "  from workflow w where w.id_processo=7 and w.cd_laudo=pp.id_status) as ds_status, ")
        sb.Append(" conc.nu_cnpj ||' - '|| conc.sg_concessionario ||' - '|| conc.nm_concessionario ||' - '|| conc.nm_cidade ||' - '|| conc.sg_uf as sg_nm_concessionario, ")
        sb.Append(" (select count(1) from ivv where dt_exclusao is null and nu_chassi = pp.ds_custom01 and dt_limite_garantia > pp.dt_pedido) as ic_tem_garantia ")
        sb.Append(" FROM  pedido_peca pp left join garantia g on pp.nu_sg = g.nu_sg, concessionario conc ")
        sb.Append(" WHERE  ")
        sb.Append(" pp.dt_exclusao is null and ")
        sb.Append(" conc.dt_exclusao is null and ")
        sb.Append(" g.dt_exclusao is null and ")
        sb.Append(" pp.cd_concessionario = conc.id_concessionario and ")
        sb.Append(" pp.id_pedido = " & idPedido.ToString)

        If HttpContext.Current.Session("nivelusuario") > 10 Then

            sb.Append(" and pp.cd_concessionario = " & HttpContext.Current.Session("concessionario"))

        End If

        cfg.Fill(ds.Tables("Pedido"), sb.ToString)
    
asked by anonymous 06.11.2014 / 20:26

1 answer

1

Your DECODE syntax is correct, missing the quotation marks in the texts. I set up an example in sqlFiddle to validate

CREATE TABLE grupo (
    cdGrupo INTEGER,
    TextoGrupo VARCHAR(100)
);

INSERT INTO grupo (cdGrupo, TextoGrupo) VALUES (1, 'Teste');
INSERT INTO grupo (cdGrupo, TextoGrupo) VALUES (2, 'Teste 2');
INSERT INTO grupo (cdGrupo, TextoGrupo) VALUES (3, 'Teste 3');
INSERT INTO grupo (cdGrupo, TextoGrupo) VALUES (4, 'Teste 4');

SELECT 
  TextoGrupo,
  cdGrupo,
  DECODE(cdGrupo,
         1, 'Peça',
         2, 'Acessorio',
         3, 'Outra coisa',
         'Indefinido') As Tipo
FROM
  grupo;

Result

TEXTOGRUPO |    CDGRUPO |   TIPO
-----------------------------------------
Teste      |    1       |   Peça
Teste 2    |    2       |   Acessorio
Teste 3    |    3       |   Outra coisa
Teste 4    |    4       |   Indefinido

SqlFiddle

    
07.11.2014 / 13:07