Pervasive SQL ignoring Order By after Group By

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Pervasive SQL ignoring Order By after Group By



I have a query where i need to group sales by sales rep then client (and currently it is ordered alphabetically sales rep then client) that part work perfectly, but the request recently came that i must sort the query SalesRep (alphabetically), Customers (based on Sales Amount Highest to lowest) and i can not get the query to do that no matter what i do within my knowlage. It just keeps on sorting by SalesRep only as far as i can see.



I'm not sure if it has something to do with the group by or something else i'm doing wrong but i'm officially lost.


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;



Below is a example of how the Query above delivers results:


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



Thank in advance for any assistance.





Can you try ordering using the full CASE expression instead of the alias?
– Tim Biegeleisen
Aug 8 at 6:26


CASE





Thanks! That seems to have worked, but WHY. Why would i need to use the entire case instead of just the alias. Is it a pervasive problem? Does it run the case twice now? If it does, doesn't that negatively affect performance?
– badboytazz
Aug 8 at 7:08






See my answer below. Not sure about performance, but a single scan over your final result set usually wouldn't be the biggest bottleneck.
– Tim Biegeleisen
Aug 8 at 7:16





It is a rather bulky query and runs about 2minutes for just one month (it is the sheer amount of entries in the HistoryLines table), so if it had to run the case twice it could become a rather lengthy afair. I'll test the time it takes now.
– badboytazz
Aug 8 at 7:24





Can you try using a single word for the alias, e.g. myalias, and then ordering by that alias?
– Tim Biegeleisen
Aug 8 at 8:23


myalias




1 Answer
1



Try using the full CASE expression in the ORDER BY clause:


CASE


ORDER BY


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;



I am assuming that the reason you can't use the alias is your database does not support it. Not all SQL databases support using an alias in the ORDER BY clause.


ORDER BY



What I think is happening here is that you are ordering by the string literal 'Product Amount', i.e. the intended alias is just being viewed a string. This is why the query is running without error.


'Product Amount'



Edit:



Based on your comments/testing, your database does support using aliases in the ORDER BY clause. But, you actually presented a string literal. There might be a way to escape Product Amount to use it directly.


ORDER BY


Product Amount






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS