Ranking Items with Multiple Sorting

Sort the items based on the following order:

  1. Qty Sold -> Descending
  2. Qty Available > 2 -> Descending
  3. Qty Available <= 2 and Status = A,B -> Descending
  4. Qty Available <= 2 and Status = S -> Descending
  5. Brand in the following order:
    • Bosch
    • LG Appliances
    • GE Appliances
    • GE Profile
    • Cafe
    • Samsung
    • Thermador
    • Monogram
    • Other Brands

Lower to a higher price (Special price) within the brands above.

ROW_NUMBER() OVER (     ORDER BY         

/* Sort by the sum of 'transaction.quantity' in descending order */
SUM( {transaction.quantity}) DESC,         

/* Sort by the maximum 'quantityavailable' when greater than 2 in descending order */
MAX(CASE WHEN {quantityavailable} > 2 THEN {quantityavailable} ELSE 0 END) DESC,         

/* Sort by the maximum 'quantityavailable' when 'custitem_aha_item_status' is 'A' or 'B' and 'quantityavailable' is less than or equal to 2 in descending order*/
MAX(CASE WHEN {custitem_aha_item_status} IN ('A', 'B') AND {quantityavailable} <= 2 THEN {quantityavailable} ELSE 0 END) DESC,         

/* Sort by the maximum 'quantityavailable' when 'custitem_aha_item_status' is 'S' and 'quantityavailable' is less than or equal to 2 in descending order */
MAX(CASE WHEN {custitem_aha_item_status} IN ('S') AND {quantityavailable} <= 2 THEN {quantityavailable} ELSE 0 END) DESC,   

/* Sort by the 'custitem_aha_sales_price' when 'cseg_aha_brand' is aforemnetioned order in ascending order */
MAX(CASE             WHEN {cseg_aha_brand}='Bosch' THEN {custitem_aha_sales_price} END) ASC,    
MAX(CASE             WHEN {cseg_aha_brand}='LG Appliances' THEN {custitem_aha_sales_price} END) ASC, 
MAX(CASE             WHEN {cseg_aha_brand}='GE Appliances' THEN {custitem_aha_sales_price} END) ASC, 
MAX(CASE             WHEN {cseg_aha_brand}='GE Profile' THEN {custitem_aha_sales_price} END) ASC, 
MAX(CASE             WHEN {cseg_aha_brand}='Cafe' THEN {custitem_aha_sales_price} END) ASC, 
MAX(CASE             WHEN {cseg_aha_brand}='Samsung' THEN {custitem_aha_sales_price} END) ASC, 
MAX(CASE             WHEN {cseg_aha_brand}='Thermador' THEN {custitem_aha_sales_price} END) ASC, 
MAX(CASE             WHEN {cseg_aha_brand}='Monogram' THEN {custitem_aha_sales_price} END) ASC, 
MAX(CASE             WHEN {cseg_aha_brand}NOT IN('Bosch','LG Appliances', 'GE Appliances','GE Profile','Cafe','Samsung','Thermador','Monogram') THEN {custitem_aha_sales_price} END) ASC)

Leave a comment

Your email address will not be published. Required fields are marked *