Estou com o erro ''The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.'' alguem pode me ajudar?

Leticia Paes 0 Reputation points
2024-02-29T13:33:35.3433333+00:00

Estou realizando a consulta abaixo e está sendo apresentado o erro no Order By do Subselect que fiz, não consigo ver uma outra forma dos dados serem apresentados.

SET NOCOUNT ON;
 
   SELECT COALESCE(PDV01.PdvEmpCod, '') as 'EMPRESA', 
          COALESCE(PDV01.PdvFilCod, '') as 'Filial', 
		  COALESCE(CONVERT(VARCHAR, PDV01.PdvEmi, 103), '') as 'DATA', 
          COALESCE(PDV01.PdvPfxCod, '') as 'PREFIXO', 
          COALESCE(PDV01.PdvCod, '') AS 'Nº PEDIDO', 
		  pdv01.PdvCpgCod as 'CÓD FORMA DE PAGAMENTO',
          RTRIM(LTRIM(cpg01.CpgNom)) as ' CONDIÇÃO PAGAMENTO',
          PDV02.PdvProCod AS 'CÓD PRODUTO' , 
          RTRIM(LTRIM(PDV02.PdvNomPro)) AS 'PRODUTO' , 
		  PDV02.PdvOprPfx 'PFX OP',
		  PDV02.PdvOprCod 'N° OP',
		   (
			SELECT
			OPR09.OprICenCod
			FROM OPR09 
			WHERE OprISeq <= OprISeq order by OprISeq desc 
		  ) AS 'DEPARTAMENTO',
		  PDV02.PdvGddCod1 as 'COR',
		  PDV02.PdvGddCod2 AS 'FURAÇÃO',
		  PDV02.PdvGddCod3 AS 'ACABAMENTO',
		  PDV02.PdvQtdRea AS 'QUANTIDADE',
          FORMAT(PDV02.PdvPrcPro, 'C', 'pt-br') AS 'PREÇO', 
		  FORMAT(PDV02.PdvtotItm, 'C', 'pt-br') as 'TOTAL',
		  CONVERT(VARCHAR, PDV02.PdvPrvEnt, 103) as 'PREVISÃO DE ENTREGA'
     FROM PDV02 
LEFT JOIN PDV01 
   ON  PDV01.PdvEmpCod = PDV02.PdvEmpCod
   AND PDV01.PdvFilCod = PDV02.PdvFilCod
   AND PDV01.PdvPfxCod = PDV02.PdvPfxCod
   AND PDV01.PdvCod    = PDV02.PdvCod
LEFT JOIN CPG01
   ON PDV01.PdvCpgCod = CPG01.CpgCod
LEFT JOIN PRO01
   ON  PDV02.PdvProCod = PRO01.ProCod
LEFT JOIN CEN01
   ON PRO01.Procencod = CEN01.CenCod
LEFT JOIN  OPR09
   ON OPR09.OprAEmpCod = PDV02.PdvEmpCod
   AND OPR09.OprAFilCod = PDV02.PdvFilCod
   AND OPR09.OprACod = PDV02.PdvOprCod
   WHERE PdvConfer= 7 or Pdvconfer = 6 

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,695 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 110.4K Reputation points MVP
    2024-02-29T22:38:15.6433333+00:00

    Por preguntas em portogues, usar https://learn.microsoft.com/pt-br/answers/questions/, faz favor. Aquì es por preguntas in inglese. There is a stray ORDER BY in your query. Just double-click on the error message to find the line. Or search for ORDER BY. Remove that ORDER BY. Or add a TOP(1) to the subquery. (I have no idea what you are trying to achieve with this query.)

    0 comments No comments

  2. LiHongMSFT-4306 26,791 Reputation points
    2024-03-01T01:30:49.0933333+00:00

    Hi @Leticia Paes

    The Order By here means nothing, just delete it.

    User's image

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.