Error - Query: The subquery returned more than 1 value

0

I'm new to SQL and I have the following error in my query: "The subquery returned more than 1 value. This is not allowed when the subquery follows a =,! =, & lt ;, < =, & gt ;, > = or when it is used as an expression ".

The query is this:

SELECT distinct 
    (PP.ProjDesc),
    U1.UsuRazaoSocial,
    C.CompDesc QtdeCNPJs,
    U1.UsuUF,
    T.TarID,
    U.UsuNome 'Responsável',
    CONVERT(DATE, PP.DataInicio, 103) Data_Início,
    CONVERT(DATE, PP.DataPrevLiberacao,103) Data_Fim,
    DATEDIFF(DAY,PP.DataInicio ,GETDATE()) Dias_Decorridos,
    SLA_Projeto = 
        CASE WHEN PP.DataPrevLiberacao >= GETDATE() THEN 
            'Dentro do Prazo' 
        ELSE 
            'FORA do Prazo' 
        END,
    (SELECT 
         COUNT(T.TarID) 
     FROM Projetos P
     INNER JOIN Tarefa T ON P.ProjID = T.ProjID
     WHERE P.ProjID = PP.ProjID) QtdeTarefas,
    (SELECT 
         COUNT(T.TarID) 
     FROM Projetos P
     INNER JOIN Tarefa T ON P.ProjID = T.ProjID
     WHERE T.TarStatus = 9
     AND P.ProjID = PP.ProjID) QtdeConcluidas,
    (SELECT 
          Faturado = 
              CASE T1.TarEstagioID WHEN 112 THEN 
                  'SIM' 
              ELSE 
                  'NÃO' 
              END
      FROM Projetos P
      INNER JOIN Tarefa T1 ON P.ProjID = T1.ProjID
      WHERE T1.TarStatus = 9
      AND T1.TarTitulo = 'Treinamento Realizado - Emitir Boleto Setup'
      AND P.ProjID = PP.ProjID
      AND T1.TarTipID = 674) Faturado,
     (SELECT 
          COUNT(T.TarID) 
      FROM Projetos P
      INNER JOIN Tarefa T ON P.ProjID = T.ProjID
      WHERE T.TarStatus = 1
      AND P.ProjID = PP.ProjID) QtdeAtendimento,
     (SELECT 
          COUNT(T.TarID) 
      FROM Projetos P
      INNER JOIN Tarefa T ON P.ProjID = T.ProjID
      WHERE T.TarStatus NOT IN (1,9)
      AND P.ProjID = PP.ProjID) QtdePendentes,
     (SELECT 
          COUNT(T.TarID) 
      FROM Projetos P
      INNER JOIN Tarefa T ON P.ProjID = T.ProjID
      WHERE T.TarStatus = 5
      AND T.TarEstagioID IN (160)
      AND P.ProjID = PP.ProjID) QtdePendentesCliente,
     (SELECT 
          COUNT(T.TarID) 
      FROM Projetos P
      INNER JOIN Tarefa T ON P.ProjID = T.ProjID
      WHERE T.TarStatus = 0
      AND T.TarEstagioID IN (8)
      AND P.ProjID = PP.ProjID) QtdePendentesOperadora,
     (SELECT 
          COUNT(T.TarID) 
      FROM Projetos P
      INNER JOIN Tarefa T ON P.ProjID = T.ProjID
      WHERE T.TarStatus = 0
      AND T.TarEstagioID IN (114)
      AND P.ProjID = PP.ProjID) QtdePendentesBoavista
  FROM PROJETOS PP
  INNER JOIN Tarefa T ON PP.ProjID = T.ProjID
  INNER JOIN Usuario U ON T.UsuIDResponsavel = U.UsuID
  INNER JOIN Usuario U1 ON T.UsuIDCliente = U1.UsuID
  LEFT JOIN Complemento C ON C.UsuID = T.UsuIDCliente and C.CompID = 1
  WHERE T.Macprocid = 33
  AND T.TarTipID = 866
    
asked by anonymous 14.07.2017 / 14:16

1 answer

1

The problem is that your subquery to return the faturado field is returning more than one value. You can use DISTINCT if you ensure that query is correct:

...

(SELECT DISTINCT CASE T1.TarEstagioID WHEN 112 THEN 
              'SIM' 
          ELSE 
              'NÃO' 
          END
  FROM Projetos P
  INNER JOIN Tarefa T1 ON P.ProjID = T1.ProjID
  WHERE T1.TarStatus = 9
  AND T1.TarTitulo = 'Treinamento Realizado - Emitir Boleto Setup'
  AND P.ProjID = PP.ProjID
  AND T1.TarTipID = 674) AS Faturado

...
    
14.07.2017 / 16:21