执行查询时,UNION,INTERSECT或EXCEPT运算符必须具有相同数量的表达式错误

时间:2017-04-12 18:19:22

标签: sql sql-server

此查询返回已打开超过72小时的故障单数。 需要了解此错误的原因以及此帖子的可能解决方案。有人可以帮忙吗?

运行此查询时出现以下错误:

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.

以下是代码:

 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] <= 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] <= 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] <= 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)]

1 个答案:

答案 0 :(得分:2)

这就是为什么我建议你可能想重新开始。您可以使用以下代码生成&#34; temp1&#34;

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] <= 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

这一切都可以简化为一个简单的查询。

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 temp1
GROUP BY temp1.[Problem_Type_Name(Child)]
    , temp1.REPORT_DATE
    , temp1.CLOSE_DATE
    , temp1.[Assigned Tech]
    , temp1.NAME
    , temp1.Job_ticket_id

您可以对temp2执行相同类型的简化。总的来说,这比它需要的更复杂。

- 编辑 -

这是你的第二个查询,其中大部分噪音已被删除,因此你可以使用骨架。

SELECT t.[Problem_Type_Name(Parent)]
    , t.[Problem_Type_Name(Child)]
    , CASE WHEN Sum(t.Total_tickets) = 0 THEN NULL /*bunch of stuff here removed*/END AS Plan_val
    , 0 AS actual
FROM   
--[A bunch of stuff here]

UNION
SELECT t.[Problem_Type_Name(Parent)]
    --Where is [Problem_Type_Name(Child)]??
    , 0 AS plan_val
    , Cast(0/*bunch of stuff here removed*/ AS INT) AS actual
FROM 
(
---A bunch of stuff here]
) t
GROUP  BY t.[Problem_Type_Name(Parent)]
    , t.[Problem_Type_Name(Child)]) temp2 

我真的建议使用合理的列名。避免使用括号,保留字和其他令人痛苦的字符。列名称用于开发人员,如果要提供应在应用程序中执行的更具描述性的名称或格式。

例如。而不是&#34; Problem_Type_Name(Parent)&#34;怎么样像ProblemTypeParent。这仍然很清楚这意味着什么,但删除了所有的丑陋和要求在名称周围使用[]。有些人喜欢下划线和其他人不喜欢。我觉得我不喜欢额外的两次按键,但收效甚微。我偶尔会使用它们,但并非总是如此。我倾向于喜欢帕斯卡尔案。但那些东西都是偏好。最重要的是保持一致。