【问题标题】:Can someone please explain how this sql query is working. The order by portion有人可以解释一下这个 sql 查询是如何工作的。按部分排序
【发布时间】:2021-05-03 22:49:29
【问题描述】:

**Question - -- 为每个客户返回客户 ID 和区域 -- 按区域对输出中的行进行排序 -- 让 NULL 排在最后(在非 NULL 值之后)

select custid
     , region
  from sales.Customers
 order 
    by case when region is null then 1 else 0 end
     , region

【问题讨论】:

  • case when region is null then 1 else 0 endregionNULL 时产生1,否则产生0。并且由于1 > 0 将在非空值之后排序空。如果那是你不明白的。否则,您需要更准确地了解您实际上不了解的内容。

标签: mysql sql-order-by


【解决方案1】:

将查询更改为:

select custid
     , region
     , case when region is null then 1 else 0 end sorter
  from sales.Customers

现在你能看到它在做什么了吗?

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2011-07-14
    • 2011-09-12
    相关资源
    最近更新 更多