代码之家  ›  专栏  ›  技术社区  ›  Sai Kumar

排序列并在排序后自定义它

  •  0
  • Sai Kumar  · 技术社区  · 7 年前

    我把桌子分类 total_customers DESC 但我想让另一个在乡下呆在最底层?

        country        total_customers  total_sales avg_sales   avg_order_val
    0   Other                15          1094.94    7.448571    72.996000
    1   USA                  13          1040.49    7.942672    80.037692
    2   Canada               8            535.59    7.047237    66.948750
    3   France               5            389.07    7.781400    77.814000
    4   Brazil               5            427.68    7.011148    85.536000
    5   Germany              4            334.62    8.161463    83.655000
    6   United Kingdom       3            245.52    8.768571    81.840000
    

    期望输出:

        country        total_customers  total_sales avg_sales   avg_order_val
    0   USA                  13          1040.49    7.942672    80.037692
    1   Canada               8            535.59    7.047237    66.948750
    2   France               5            389.07    7.781400    77.814000
    3   Brazil               5            427.68    7.011148    85.536000
    4   Germany              4            334.62    8.161463    83.655000
    5   United Kingdom       3            245.52    8.768571    81.840000
    6   Other                15          1094.94    7.448571    72.996000
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   jpw    7 年前

    ORDER BY CASE country WHEN 'Other' THEN 1 ELSE 0 END, total_customers DESC
    
        2
  •  1
  •   sticky bit    7 年前

    尝试此查询

    SELECT * FROM total_customers ORDER BY (CASE WHEN country = 'Other' THEN 1 ELSE 0 END), country  DESC