用ORDER BY SUM进行UNION查询

时间:2018-11-09 02:05:42

标签: sql database sql-order-by union access

这是我想做的事情:

SELECT 
  TOP 25 tblTasks.Task, 
  tblTasks.Task_Status, 
  tblTasks.Award_Date, 
  tblTasks.End_Date, 
  Sum(tblPRs.Ceiling_Final) AS SumOfCeiling_Final, 
  tblTasks.Program, tblTasks.Prime, tblClient.Org_Top
FROM 
  (tblTasks INNER JOIN tblClient ON tblTasks.Task_ID = tblClient.Task_ID) 
   INNER JOIN tblPRs ON tblTasks.Task_ID = tblPRs.Task_ID
GROUP BY 
   tblTasks.Task, 
   tblTasks.Task_Status, 
   tblTasks.Award_Date, 
   tblTasks.End_Date, 
   tblTasks.Program, 
   tblTasks.Prime, tblClient.Org_Top
HAVING 
   (((tblTasks.Task_Status)="open"))

UNION

SELECT 
   TOP 25 tblTasks.Task, 
   tblTasks.Task_Status, 
   tblTasks.Award_Date, 
   tblTasks.End_Date, Sum(tblPRs.Ceiling_Final) AS SumOfCeiling_Final, 
   tblTasks.Program, 
   tblTasks.Prime, tblClient.Org_Top
FROM 
   (tblTasks INNER JOIN tblClient ON tblTasks.Task_ID = tblClient.Task_ID) 
    INNER JOIN tblPRs ON tblTasks.Task_ID = tblPRs.Task_ID
GROUP BY 
    tblTasks.Task, 
    tblTasks.Task_Status, 
    tblTasks.Award_Date, 
    tblTasks.End_Date, 
    tblTasks.Program, 
    tblTasks.Prime, 
    tblClient.Org_Top
HAVING 
    (((tblTasks.Task_Status)="pending"))

ORDER BY 
    Sum(tblPRs.Ceiling_Final) DESC;

我正在将SQL输入MS Access。这两个查询之间的唯一区别是HAVING参数值。各个查询来自MS Access查询设计。与ORDER BY语句配对时,每个单独的查询都能正常工作,并为我提供预期的结果。当我尝试将它们都作为UNION运行时,出现以下错误:

  

ORDER BY表达式(Sum(tblPRs.Ceiling_Final))包括字段   查询未选择的内容。只有那些在   第一个查询可以包含在ORDER BY表达式中。

3 个答案:

答案 0 :(得分:1)

nb:我假设您只想获得SumOfCeiling_Final最高的25行

我建议您首先使用union all形成“派生表”,然后再寻找top n行。请注意,having clause不能代替where clause。如果过滤条件需要汇总值(例如having clause),则having sum(cost) > 1000是必需的。如果筛选条件不涉及聚合,则将其放在where clause中。

SELECT TOP 25
    *
FROM (
    SELECT
        tblTasks.Task, tblTasks.Task_Status, tblTasks.Award_Date, tblTasks.End_Date, SUM(tblPRs.Ceiling_Final) AS SumOfCeiling_Final, tblTasks.Program, tblTasks.Prime, tblClient.Org_Top
    FROM (tblTasks
    INNER JOIN tblClient
        ON tblTasks.Task_ID = tblClient.Task_ID)
    INNER JOIN tblPRs
        ON tblTasks.Task_ID = tblPRs.Task_ID
    WHERE tblTasks.Task_Status = "open"
    GROUP BY
        tblTasks.Task
       ,tblTasks.Task_Status
       ,tblTasks.Award_Date
       ,tblTasks.End_Date
       ,tblTasks.Program
       ,tblTasks.Prime
       ,tblClient.Org_Top

    UNION ALL

    SELECT
        tblTasks.Task, tblTasks.Task_Status, tblTasks.Award_Date, tblTasks.End_Date, SUM(tblPRs.Ceiling_Final) AS SumOfCeiling_Final, tblTasks.Program, tblTasks.Prime, tblClient.Org_Top
    FROM (tblTasks
    INNER JOIN tblClient
        ON tblTasks.Task_ID = tblClient.Task_ID)
    INNER JOIN tblPRs
        ON tblTasks.Task_ID = tblPRs.Task_ID
    WHERE tblTasks.Task_Status = "pending"
    GROUP BY
        tblTasks.Task
       ,tblTasks.Task_Status
       ,tblTasks.Award_Date
       ,tblTasks.End_Date
       ,tblTasks.Program
       ,tblTasks.Prime
       ,tblClient.Org_Top
    ) d
ORDER BY
    SumOfCeiling_Final DESC
;

NB UNION ALL不会删除重复的行,UNION会删除重复的行。因此,UNION ALL实际上可以使LESS正常工作,即速度更快。此外,在上面的查询中,每行必须包含Task_Status =“ open”,而在下面的行中必须包含Task_Status =“ pending”,因此2个查询所产生的行必须不同。尽管没有数据可以验证,但我不确定,看来您只需要使用union all

答案 1 :(得分:0)

我相信以下其中一项可以实现您所期望的样子:

SELECT *
FROM (put your 1st query here
      UNION
      put your 2nd query here)
ORDER BY SumOfCeiling_Final DESC;

-或-

SELECT * FROM (put your 1st query here, and include ORDER BY Sum(tblPRs.Ceiling_Final) DESC)
UNION
SELECT * FROM (put your 2nd query here, and include ORDER BY Sum(tblPRs.Ceiling_Final) DESC)

我的sql-fu不够强大,无法确定它们是否可以正常工作,而不会摆弄实际数据;说到哪一个,您可以使用最少的架构和数据来设置SQL Fiddle来说明您的问题吗?

编辑:为了记录,我也喜欢@Used_By_Already发布的答案,显然他/她的答案与我的第一个选择非常相似;使用最适合您需求的产品。

答案 2 :(得分:0)

我最终将每个查询放入派生表中,然后对派生表执行UNION。

SELECT *
FROM ( placed first query here including the ORDER BY
) AS a

UNION ALL

SELECT *
FROM ( placed second query here including the ORDER BY
) AS b;