End of file, incorrect SQL SERVER syntax

Question:

I'm trying to run a command in my sql, but whenever I try it I get an End of File error, I've looked line by line and found nothing, 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)
      ) 

Answer:

You created a WITH nothing in your code. WITH just groups the information in the alias you determined, what to do with it who determines is you:

WITH subresults AS (
  -- ...
)
SELECT *
  FROM subresults;
Scroll to Top
AllEscort