Not bringing the records - Query SQL

Asked

Viewed 60 times

0

Good afternoon!

Guys, below is the query that is to bring the amount of active clients in one column and the amount of locked clients in another column, however, when running the query, comes null in both columns. OBS: When executing the separate sub-consultations, the quantity comes normally.

                               --Quantidade de Clientes Bloqueados e Ativos--
SELECT 
BLOQUEADOS. [Quantidade de Clientes Bloqueados],
ATIVOS. [Quantidade de Clientes Ativos]
FROM
(SELECT COUNT(DISTINCT T.UsuIDCliente) [Quantidade de Clientes Bloqueados]
  FROM Tarefa T
  LEFT JOIN Usuario U
  ON U.UsuID = T.UsuIDCliente
  INNER JOIN ParametroProdutoCliente PPC ON U.UsuID = PPC.UsuID
 WHERE T.TarTitulo = 'Bloquear Cliente - Inadimplente'
   AND T.TarTipID = 572
   AND PPC.ProID = 2
   AND NOT EXISTS(SELECT 1
                    FROM Tarefa t2
                   WHERE t2.TarTitulo = 'Desbloqueio Cliente'
                     AND t2.TarTipID = 574
                     AND t2.usuidcliente  = t.usuidcliente)) BLOQUEADOS
INNER JOIN
(SELECT DISTINCT COUNT(U.UsuID) [Quantidade de Clientes Ativos]
FROM Usuario U 
INNER JOIN ParametroProdutoCliente PPC ON U.UsuID = PPC.UsuID
LEFT JOIN CurvaABC ABC ON U.CurvaID = ABC.CurvaID
LEFT JOIN Solicitacao S ON U.UsuID = S.UsuIDCliente
LEFT JOIN FRM_3 F3 ON S.SolID = F3.ChamadoID
LEFT JOIN ComplementoColunas CC ON U.UsuID = CC.UsuID AND CC.CompID = 32 AND CC.ColunaID = 12
LEFT JOIN Regioes R ON U.EstadoID = R.EstadoID
LEFT JOIN CRMDetalhesConta DC ON U.UsuID = DC.ContaID
LEFT JOIN CRMRamo RM ON DC.RamoID = RM.RamoID
WHERE U.UsuTipo = 'C'
      AND PPC.ProID = 2
      AND S.SolCaminho = 'Implantação Boavista'
      AND F3.C03 IS NOT NULL
      AND U.EmpLiberada = 1) ATIVOS
ON BLOQUEADOS.[Quantidade de Clientes Bloqueados] = ATIVOS.[Quantidade de Clientes Ativos]
  • puts the structure of tables, ER model, anything that facilitates

  • No need, must be some parameter in Inner Join (I believe) that is not working. Remembering that if I run the sub-consultations, it runs normally.

  • 1

    If you need two columns, to show only active/inactive amount, why this lot of Join ? what is the condition that puts the client as active or not ? what is the relationship between the tables ? the structure serves to help this part

1 answer

1


If your two sub-queries already do everything you want, no need to join in the two.

Besides you are doing the Join of the two with the columns of quantity Blocked and Active, which in my point of view will hardly bring results.

Take out the JOIN and trade it for a comma , and take out the ON too.

Type:

SELECT 
BLOQUEADOS. [Quantidade de Clientes Bloqueados],
ATIVOS. [Quantidade de Clientes Ativos]
FROM
(SELECT COUNT(DISTINCT T.UsuIDCliente) [Quantidade de Clientes Bloqueados]
  FROM Tarefa T
  LEFT JOIN Usuario U
  ON U.UsuID = T.UsuIDCliente
  INNER JOIN ParametroProdutoCliente PPC ON U.UsuID = PPC.UsuID
 WHERE T.TarTitulo = 'Bloquear Cliente - Inadimplente'
   AND T.TarTipID = 572
   AND PPC.ProID = 2
   AND NOT EXISTS(SELECT 1
                    FROM Tarefa t2
                   WHERE t2.TarTitulo = 'Desbloqueio Cliente'
                     AND t2.TarTipID = 574
                     AND t2.usuidcliente  = t.usuidcliente)) BLOQUEADOS
,
(SELECT DISTINCT COUNT(U.UsuID) [Quantidade de Clientes Ativos]
FROM Usuario U 
INNER JOIN ParametroProdutoCliente PPC ON U.UsuID = PPC.UsuID
LEFT JOIN CurvaABC ABC ON U.CurvaID = ABC.CurvaID
LEFT JOIN Solicitacao S ON U.UsuID = S.UsuIDCliente
LEFT JOIN FRM_3 F3 ON S.SolID = F3.ChamadoID
LEFT JOIN ComplementoColunas CC ON U.UsuID = CC.UsuID AND CC.CompID = 32 AND CC.ColunaID = 12
LEFT JOIN Regioes R ON U.EstadoID = R.EstadoID
LEFT JOIN CRMDetalhesConta DC ON U.UsuID = DC.ContaID
LEFT JOIN CRMRamo RM ON DC.RamoID = RM.RamoID
WHERE U.UsuTipo = 'C'
      AND PPC.ProID = 2
      AND S.SolCaminho = 'Implantação Boavista'
      AND F3.C03 IS NOT NULL
      AND U.EmpLiberada = 1) ATIVOS
  • Thanks man! That’s right, I missed my attention, rsrs. Thank you

Browser other questions tagged

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