End of file, incorrect SQL SERVER syntax

Asked

Viewed 16 times

3

I am trying to execute a command in my sql, but always when I try it turns me an End of File error, I already looked line by line and nothing figured out, how can I solve this problem ?

DECLARE @anohref int 
    SET @anohref = YEAR(GETDATE()) 
    DECLARE @pastanohref int 
    SET @pastanohref = YEAR(GETDATE()) - 1 


    SELECT SUM(total) total 
    FROM( 
      SELECT sum(WorkOrder) AS total from WorkOrder WHERE workType = '02' AND businessUnit = 'MM' and year(conclusionDate) = @anohref
      UNION ALL
      SELECT sum(WorkOrder) AS total from WorkOrder WHERE workType = 'CORRETIVA' AND businessUnit = 'MM' and year(conclusionDate) = @anohref
    ) s

    UNION ALL


    select sum(total) total
    from (
      select sum(workOrder) as total from workOrder where workType = '02' and year(workOrderDate)=2018
      union all
      select sum(workOrder) as total from workOrder where workType= 'corretiva' and year(workOrderDate)=2018
    ) s ;

    WITH subresults AS
      ( 
        SELECT MES = MONTH(workOrderDate),
          preventiva = 
                (SUM(CASE WHEN WorkType = '02' 
                  AND workOrderDescription = 'preventiva' THEN 1.0 ELSE 0 END 
                )),
          corretivas = (SUM(CASE WHEN workOrderDescription = 'CORRETIVA' THEN 1 ELSE 0 END)), 

          total = SUM(CASE WHEN WorkType = '02' 
                AND workOrderDescription = 'preventiva' THEN 1.0 
                ELSE 0 END + CASE WHEN workOrderDescription = 'CORRETIVA' THEN 1 ELSE 0 END
               ) 
        FROM WorkOrder WHERE 
        YEAR(workOrderDate) = @pastanohref 
        GROUP BY MONTH(workOrderDate)
      ) 

1 answer

1


You created a WITH without using for anything in your code. The WITH only groups the information in the alias that you’ve determined, what to do with her determines you:

WITH subresults AS (
  -- ...
)
SELECT *
  FROM subresults;

Browser other questions tagged

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