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.
Try using the full CASE
expression in the ORDER BY
clause:
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.
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.
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.