MS Access Union,Order By搞乱我的查询订单

时间:2017-11-01 18:21:40

标签: sql ms-access sql-order-by ms-access-2007 union

SQL:

SELECT 
    SUM(dbo_SO_SalesHistory.DollarsSold) AS SumDollarsSold,  
    "PHOA" As CustomerNo
FROM  
    dbo_SO_SalesHistory
WHERE 
    ((dbo_SO_SalesHistory.[CustomerNo]) IN ("PHOM","PHOB", "PHOA", "HOMP")) 
    AND ((dbo_SO_SalesHistory.InvoiceDate) BETWEEN [BeginDate] AND [EndDate])

UNION

SELECT TOP 9 
    SUM(DollarsSold), CustomerNo 
FROM 
    dbo_SO_SalesHistory
WHERE 
    ((dbo_SO_SalesHistory.CustomerNo) NOT IN ("PHOM","PHOB", "PHOA", "HOMP")) 
    AND ((dbo_SO_SalesHistory.InvoiceDate) BETWEEN [BeginDate] AND [EndDate])
GROUP BY 
    dbo_SO_SalesHistory.CustomerNo 
ORDER BY 
    1 DESC;

当我单独运行两个查询时,查询的第二部分返回我期望的输出:

Correct

但是当我与UNION一起运行时,订单会以某种方式混淆:

WRONG

在代码ORDER BY 1 DESC中,1指的是第一列,即总和。

我试过了

ORDER BY SUM(dbo_SO_SalesHistory.DollarsSold) DESC;
相反,它会返回错误:

Err 3

任何建议都将受到高度赞赏!

更新:这是当前的查询输出:

[enter image description here]

所需的输出如下:

enter image description here

(我从图片1中剪切并粘贴了2~10行)。我希望这有助于您的理解!

我当前的查询:

Sum of Dollar | Customer
--------------+----------
     10       |    A   
      8       |    B
      1       |    Q

所需的查询输出

14 | B_Total
10 | A
 5 | C

B_Total(14)是X(4),Y(3),Z(2)和B(5)之和

更新:当前查询

SELECT 
    SumDollarsSold, CustomerNo, MyOrder 
FROM 
    (SELECT 
         SUM(dbo_SO_SalesHistory.DollarsSold) AS SumDollarsSold, 
         "PHOALLE" AS CustomerNo, 1 AS MyOrder
     FROM 
         dbo_SO_SalesHistory
     WHERE 
         ((dbo_SO_SalesHistory.[CustomerNo]) IN ("PHOMIN","PHOALLE","PHOBROO","PHOMP"))  
         AND ((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate])
     UNION
     SELECT TOP 9 
         SUM(DollarsSold), CustomerNo, 2 as MyOrder 
     FROM 
         dbo_SO_SalesHistory
     WHERE 
         ((dbo_SO_SalesHistory.CustomerNo) NOT IN ("PHOM","PHOA","PHOB","PHOM")) 
         AND ((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate])
     GROUP BY 
         dbo_SO_SalesHistory.CustomerNo
     ORDER BY 
         1 DESC
) a 
ORDER BY 
    MyOrder, SumDollarsSold DESC;

1 个答案:

答案 0 :(得分:0)

当你UNION没有保证订单时,你丢失了你在第二个查询上已经完成的ORDER BY。您可以添加另一个字段以确保第一条记录始终位于顶部,但是您确实需要在所选字段中包含该字段。像这样:

SELECT 
    SumDollarsSold, CustomerNo, MyOrder 
FROM 
    (SELECT 
         SUM(dbo_SO_SalesHistory.DollarsSold) AS SumDollarsSold, 
         "PHOA" AS CustomerNo, 1 AS MyOrder
     FROM 
         dbo_SO_SalesHistory
     WHERE 
         ((dbo_SO_SalesHistory.[CustomerNo]) IN ("PHOM","PHOB", "PHOA", "PHOM"))  
         AND ((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate])
     UNION
     (SELECT TOP 9 
         SUM(DollarsSold), CustomerNo, 2 as MyOrder 
     FROM 
         dbo_SO_SalesHistory
     WHERE 
         ((dbo_SO_SalesHistory.CustomerNo) NOT IN ("PHOM","PHOB", "PHOA", "PHOM")) 
         AND ((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate])
     GROUP BY 
         dbo_SO_SalesHistory.CustomerNo
     ORDER BY 
         SUM(DollarsSold) DESC)
) a 
ORDER BY 
    MyOrder, SumDollarsSold DESC;