Doubt - SQL Server Query Condition

3

Galera, in the query below is bringing as follows. But I want you to bring only the 'YES'. The difficulty is because it is a subselect with case. I bring you where? No where or a having? How would it be more or less

Current Result

Cliente Integrado
Sim
Não

Expected Results

Sim

Inquiry

SELECT distinct 
       (select CASE WHEN COUNT(DISTINCT f2.contaid) > 1 THEN 'SIM' ELSE 'NÃO' END as Cliente_Integrado from FRM_51 f2 where f2.C02 = erp.EmpresaERP and (f2.C05 = 3641 or f2.C06 = 3643)) Cliente_Integrado
FROM Tarefa T
LEFT JOIN FRM_52 FRM52 ON FRM52.TarefaID = T.TarID
--LEFT JOIN FRM_51 FRM51 ON FRM51.C01 = FRM52.C01
LEFT JOIN ERPAuxiliar ERP ON ERP.ERPEmpresaERP = FRM52.C01
WHERE T.ProID = 13 
      AND T.TarTipID = 667 --AND CIR.UsuNome LIKE '%FARMACIA%NACIONAL%'
      AND T.TarTitulo NOT IN ('Integração ERP Parceiro','Integração ERP Parceiro - Retorno','Integração ERP Parceiro - Remessa','Envio de Layout - Integração ERP Parceiro')
      and t.TarID not in(163388)
    
asked by anonymous 10.05.2018 / 13:48

1 answer

0

One way to get the result you expect is to use query above within subquery in FROM and apply the filter after that. Replacing it would look something like this:

SELECT *
  FROM (SELECT DISTINCT (SELECT CASE
                                  WHEN COUNT(DISTINCT f2.contaid) > 1 THEN 'SIM'
                                  ELSE 'NÃO'
                                END AS Cliente_Integrado
                           FROM frm_51 f2
                          WHERE f2.c02 = erp.empresaerp
                            AND (f2.c05 = 3641 OR f2.c06 = 3643)) AS Cliente_Integrado
          FROM tarefa t
          LEFT JOIN frm_52 frm52 ON frm52.tarefaid = t.tarid
          LEFT JOIN erpauxiliar erp ON erp.erpempresaerp = frm52.c01
         WHERE t.proid = 13
           AND t.tartipid = 667
           AND t.tartitulo NOT IN ('Integração ERP Parceiro',
                                   'Integração ERP Parceiro - Retorno',
                                   'Integração ERP Parceiro - Remessa',
                                   'Envio de Layout - Integração ERP Parceiro')
           AND t.tarid NOT IN (163388)
       ) x
 WHERE x.Cliente_Integrado = 'SIM'
    
10.05.2018 / 21:19