无处不在 SQL 在 Group By 之后忽略 Order By

Pervasive SQL ignoring Order By after Group By

我有一个查询,我需要按销售代表然后是客户对销售进行分组(目前它是按字母顺序排列的销售代表然后是客户)这部分工作得很好,但最近有人要求我必须对查询 SalesRep (按字母顺序排列),客户(基于销售额从最高到最低),无论我在我的知识范围内做什么,我都无法得到查询来做到这一点。据我所知,它只是按 SalesRep 排序。

我不确定这是否与 group by 或其他我做错的事情有关,但我正式迷路了。

    Select SM.Description as 'Sales Rep', CM.CustomerDesc, 
        sum(case when HL.ItemCode Not In ('111','112') then case when HL.DocumentType in (9,11) then HL.DiscountAmount else HL.DiscountAmount*(-1) end end) as 'Product Amount', 
        sum(case when HL.ItemCode in ('111','112') then case when HL.DocumentType in (9,11) then HL.DiscountAmount else HL.DiscountAmount*(-1) end end) as 'Transport Amount',
        max(CM.CashAccount) as CashAccount
from HistoryLines HL 
inner join SalesmanMaster SM on SM.Code = HL.SalesmanCode
inner join CustomerMaster CM on CM.CustomerCode = HL.CustomerCode
where`enter code here` HL.DocumentType in (8,9,11)
    and HL.DDate between '2018-07-01' and '2018-07-31'
group by SM.Description, CM.CustomerDesc
order by  SM.Description, 'Product Amount' DESC;

下面是上面的查询如何传递结果的示例:

Description |CustomerDesc   |Product Amount   |Transport    |Cash
A Sales Man |M Client       |17350            |3425         |0
A Sales Man |B Client       |6300             |1343         |1
B Sales Man |A Client       |8144             |1782         |0
B Sales Man |H Client       |45956.33         |13012.24     |0
B Sales Man |K Client       |34255.5          |2484         |0
B Sales Man |N Client       |96978.64         |14969.14     |0
B Sales Man |S Client       |139720.8         |0            |0
B Sales Man |TH Client      |25292.37         |17447.9      |0
B Sales Man |TY Client      |14809.6          |0            |0
B Sales Man |V Client       |11034            |6307.2       |0
C Sales Man |0 Client       |4590             |1350         |0
C Sales Man |AP Client      |23706            |3570         |0
C Sales Man |AR Client      |26106            |4950         |0
C Sales Man |BU Client      |54558.58         |0            |1
C Sales Man |C Client       |0                |0            |0
C Sales Man |CI Client      |27889.65         |4087.2       |0
C Sales Man |E Client       |8204.55          |1250         |0
C Sales Man |F Client       |72329.44         |17898        |0
C Sales Man |G Client       |4897.8           |1350         |0
C Sales Man |I Client       |15167.4          |2700         |0
C Sales Man |J Client       |274.8            |0            |0

在此先感谢您的帮助。

尝试在 ORDER BY 子句中使用完整的 CASE 表达式:

ORDER BY
    SM.Description,
    SUM(CASE WHEN HL.ItemCode NOT IN ('111','112')
             THEN CASE WHEN HL.DocumentType IN (9,11)
                       THEN HL.DiscountAmount
                       ELSE HL.DiscountAmount*(-1) END
        END) DESC;

我假设您不能使用别名的原因是您的数据库不支持它。并非所有 SQL 数据库都支持在 ORDER BY 子句中使用别名。

我认为这里发生的事情是您按字符串文字 'Product Amount' 进行排序,即预期的别名只是被视为一个字符串。这就是查询 运行 没有错误的原因。

编辑:

根据您的 comments/testing,您的数据库 支持在 ORDER BY 子句中使用别名。但是,您实际上提供了一个字符串文字。可能有一种方法可以转义 Product Amount 以直接使用它。