title: UNION, INTERSECT or EXCEPT operator must have an equal number of expressions error while executing query
body: <p>This query returns the number of tickets which have been open older than 72 hours.
  Need to understand the reason for this error and a possible solution to this post.Could anyone please help?</p>
  
  <p>I get the following error while running this query:</p>
  
  <pre><code>Msg 205, Level 16, State 1, Line 2
  All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
  </code></pre>
  
  <p>Below given is the code:</p>
  
  <pre><code> SELECT temp1.*, max(cast(Round(temp2.Plan_Val,0) as int)) as Plan_Val,max(cast(Round(temp2.actual,0) as int)) as actual_val FROM(
          SELECT t.[Problem_Type_Name(Parent)] ,t.[Problem_Type_Name(Child)], REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id FROM (
          SELECT '%Tickets Open Older than 72 Business Hours' as [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)], REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id
          FROM TEMP_TICKET_STATE
  
          UNION
  
          SELECT '%Tickets Open Older than 72 Business Hours' as [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)], REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id
          FROM TEMP_TICKET_STATE
          WHERE [greater than 72 hours] &lt;= 4320) t
  
          group by t.[Problem_Type_Name(Parent)] , t.[Problem_Type_Name(Child)],REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id) temp1
  
          INNER JOIN
  
  
          (SELECT t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)],case when sum(t.Total_tickets) = 0 then null else cast(Round((cast(sum(t.Total_tickets) as decimal(38,2))- cast(sum(t.Alarm_Val) as decimal(38,2)))/cast(sum(t.Total_tickets) as decimal(38,2))*100,0) as int) end as Plan_val, 0 as actual  FROM (
          SELECT '%Tickets Open Older than 72 Business Hours' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],0 as Alarm_Val, count(distinct [Job_ticket_id])/29 as Total_tickets 
          FROM TEMP_TICKET_STATE
          group by [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] 
  
          UNION
  
          SELECT '%Tickets Open Older than 72 Business Hours' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],count(distinct [Job_ticket_id])/29 as Alarm_Val, 0 as Total_tickets 
          FROM TEMP_TICKET_STATE
          WHERE [greater than 72 hours] &lt;= 4320
          group by [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] ) t
          group by t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)] 
  
          UNION
          SELECT t.[Problem_Type_Name(Parent)],0 AS plan_val, cast(Round((cast(sum(t.Total_tickets) as decimal(38,2))- cast(sum(t.Alarm_Val) as decimal(38,2)))/cast(sum(t.Total_tickets) as decimal(38,2))*100,0) as int) as actual  FROM (
          SELECT '%Tickets Open Older than 72 Business Hours' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],0 as Alarm_Val, count(distinct [Job_ticket_id])/4 as Total_tickets 
          FROM TEMP_TICKET_STATE_month
          group by [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] 
  
          UNION
  
          SELECT '%Tickets Open Older than 72 Business Hours' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],count(distinct [Job_ticket_id])/4 as Alarm_Val, 0 as Total_tickets 
          FROM TEMP_TICKET_STATE_month
          WHERE [greater than 72 hours] &lt;= 4320
          group by [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] ) t
  
          group by t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)]) temp2
  
          ON temp1.[Problem_Type_Name(Parent)]=temp2.[Problem_Type_Name(Parent)]
          Group by temp1.[Assigned Tech],temp1.Close_Date,temp1.Job_ticket_id,temp1.NAME,temp1.[Problem_Type_Name(Parent)],temp1.Report_Date,temp1.[Problem_Type_Name(Child)]
  </code></pre>