PLSQL:需要帮助来理解 ORDER BY 中的 CASE 指令
我有一段代码,其中有一个带有 CASE 的 ORDER BY:
ORDER BY
(
CASE
WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
ELSE '2'
|| upper(id.name)
END) ASC,
r.date DESC ;
有人可以解释一下:
- '1AAAAA' 和 '2' 的含义是什么?
- 是什么意思 <块引用>
||上层(id.name)
I have a piece of code that had a ORDER BY with a CASE in it:
ORDER BY
(
CASE
WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
ELSE '2'
|| upper(id.name)
END) ASC,
r.date DESC ;
Could someone explain:
- what is the meaning of the '1AAAAA' and '2' ?
- what is the meaning of
|| upper(id.name)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 PL/SQL 中,
||
是连接运算符。排序的具体方式取决于查询的其余部分,但看起来它会将
r.id BETWEEN 900 AND 999
的记录放在其他记录之前,这些记录按id.name 排序
。In PL/SQL,
||
is the concatenation operator.Exactly how the ordering is happening depends on the rest of the query, but it looks like it's putting records with
r.id BETWEEN 900 AND 999
before other records, which are sorted byid.name
.case 表达式的计算结果为一个值 上述
整个代码块的计算结果为“1AAAAA”或“2[value-of-r.id]”,具体取决于
r.id
的值。由于它位于
order by
子句中,因此该值将用于对结果进行排序,如下所示:r.id
介于 900 和 999 之间的所有记录r.id
升序排列(||
是字符串连接运算符)。The case expression evaluates to a value
The whole above block of code with either evaluate to '1AAAAA', or '2[value-of-r.id]' depending on the value of
r.id
.As this is in the
order by
clause this value will be used to sort the results as follows:r.id
is between 900 and 999r.id
(the||
is the string concatenation operator).这是一些数据。正如您所看到的,名称按 ASCII 顺序排序,这与字母顺序不完全相同:
按
upper(name)
排序使其不区分大小写:CASE()
通过首先对指定 ID 范围内的所有记录进行分组,然后对所有其他记录进行分组,进一步更改了此设置。所选范围内的记录仅按日期排序,而其他记录仍按名称然后日期排序:Here is some data. As you can see the name sorts in ASCII order, which is not exactly the same as alphabetical order:
Sorting by
upper(name)
makes it case-insensitive:The
CASE()
changes this further by grouping all the records within the specfied ID range first, then all the other records. The records in the selected range are just sorted by the DATE whereas the other records are still sorted by name then date:那是文字常量。
||
是 SQL 标准串联运算符。 'A' || “B”产生“AB”。恕我直言,您的问题是整个
order by case
的含义,因此,请逐步进行:这将按 case 表达式评估的结果(上升)对结果集进行排序,然后按 r.date (后裔)。
对于 900 到 999 之间的任何 ID,该案例将仅返回“1AAAAA”(这将按 r.date 排序,还记得吗?”
对于任何其他值,它将在 id.name 之前连接 2。
这可确保任何具有 id 的记录900 到 999 之间出现在第一个“组”中,该组仅按日期降序排列,然后第二组将包含所有其他记录,按名称的大写字母排序,然后按日期排序
。这些数据以了解其工作原理...只需将 case 表达式作为新列添加到您的 select 语句中,
例如,如果您的查询如下所示:
添加这样的 case:
这将帮助您了解该表达式发生了什么。 ,因为您将看到生成的数据作为查询的最后一列。
That are literal constants.
||
is the SQL standard concatenation operator. 'A' || 'B' produces 'AB'.IMHO, your question is what the entire
order by case
means, so, go step by step:This will order your result set by the result of the case expression evaluation (ascendant), then by r.date (descendant).
The case will just return '1AAAAA' for any ID between 900 and 999 (this will then be ordered by r.date, remember?'
For any other value, it will concatenate 2 before the id.name.
This ensures any record with id between 900 and 999 to appear in the first "group", which is ordered just by date, descending. Then a second group will contain all the other records, ordered by the upper of name, then by the date.
You may want to see this data to understand how this works... just add the case expression to your select statement as a new column.
For example if your query starts like this:
add the case like this:
This will help you understand what's going on with that expression, as you will see the produced data as the last column of your query.