2

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.

badboytazz
  • 23
  • 2
  • Can you try ordering using the full `CASE` expression instead of the alias? – Tim Biegeleisen Aug 08 '18 at 06:26
  • 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 08 '18 at 07: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 08 '18 at 07: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 08 '18 at 07:24
  • Can you try using a single word for the alias, e.g. `myalias`, and then ordering by that alias? – Tim Biegeleisen Aug 08 '18 at 08:23
  • It Works now with the alias one word and the '' removed. Ok feeling really stupid now. I've been working with Pervasive for almost a year now doing small queries and i never realized that 'alias' is different from alias (without the ''). I initially also thought that the problem was the space in the alias so i changed it to 'my_alias' instead of 'my alias' but i never removed the '' since that is how i was taught to do aliases in pervasive and i never questioned it since it always worked. Thanks for opening my eyes to a new possibilities. – badboytazz Aug 08 '18 at 10:35

1 Answers1

1

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360