Error - Query: The sub-query returned more than 1 value

Asked

Viewed 3,060 times

0

I am new to SQL and I have the following error in my query: "The sub-query returned more than 1 value. This is not allowed when the subconsulta follows a =, != , <, <= , >, >= 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
  • the problem seems to be in that subselect (SELECT Faturado = CASE T1.TarEstagioID WHEN 112 THEN 'SIM' ELSE 'NÃO' END&#xA;FROM Projetos P&#xA;INNER JOIN Tarefa T1 ON P.ProjID = T1.ProjID&#xA;WHERE T1.TarStatus = 9&#xA;AND T1.TarTitulo = 'Treinamento Realizado - Emitir Boleto Setup'&#xA;AND P.ProjID = PP.ProjID&#xA;AND T1.TarTipID = 674) Faturado, that can return more than one line

  • if I run only this sub-volume, it normally generates

  • By the way, apologies, it really does occur an error when I run this sub-concession. Error is: Msg 4104, Level 16, State 1, Line 6 Multi-part identifier "PP.Projid" could not be associated.

  • it is not the query that is wrong, and you cannot execute it only because it depends on the values of the higher queries. The problem is that it can return more than one line, which cannot happen in sub-consultations. try to put it like this:

  • (SELECT TOP 1 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,

  • 1

    @Renanbessa: What is the result that the query should return? // I realize that there is project and that a project can be composed of one or more tasks. And that it is necessary to obtain summary information about each project. But there is a mixture of synthetic report (project accumulators, for example), with analytical (individual information of each task). // What is the rule for calculating the value of the Invoiced column? The information must be generated per project or per project/task?

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

Show 2 more comments

1 answer

1

The problem is that your subquery to return the field faturado is returning more than one value. You can use a DISTINCT if it ensures that the outcome of query that 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

...

Browser other questions tagged

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