PLSQL:需要帮助来理解 ORDER BY 中的 CASE 指令

发布于 2024-10-29 07:21:28 字数 533 浏览 1 评论 0原文

我有一段代码,其中有一个带有 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 ;

有人可以解释一下:

  1. '1AAAAA' 和 '2' 的含义是什么?
  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:

  1. what is the meaning of the '1AAAAA' and '2' ?
  2. what is the meaning of

    || upper(id.name)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千年*琉璃梦 2024-11-05 07:21:28

在 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 by id.name.

一花一树开 2024-11-05 07:21:28

case 表达式的计算结果为一个值 上述

CASE
  WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
  ELSE '2'
    || upper(id.name)
END

整个代码块的计算结果为“1AAAAA”或“2[value-of-r.id]”,具体取决于 r.id 的值。

由于它位于 order by 子句中,因此该值将用于对结果进行排序,如下所示:

  1. 首先列出 r.id 介于 900 和 999 之间的所有记录
  2. ,然后列出所有记录其他记录按r.id升序排列(||是字符串连接运算符)。

The case expression evaluates to a value

CASE
  WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
  ELSE '2'
    || upper(id.name)
END

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:

  1. first list all records where r.id is between 900 and 999
  2. then list all other records in ascending order of r.id (the || is the string concatenation operator).
忆悲凉 2024-11-05 07:21:28

这是一些数据。正如您所看到的,名称按 ASCII 顺序排序,这与字母顺序不完全相同:

SQL> select id, name, somedate
  2  from t42
  3  order by name, somedate
  4  /

        ID NAME       SOMEDATE
---------- ---------- ---------
         8 Billington 24-MAR-11
        13 Cave       19-MAR-11
         4 Clarke     28-MAR-11
       919 Feuerstein 13-MAR-11
        16 Gasparotto 16-MAR-11
      1014 KULASH     18-MAR-11
         1 Kestelyn   31-MAR-11
       917 Kishore    15-MAR-11
         2 Lira       30-MAR-11
         6 PADFIELD   26-MAR-11
        11 Rigby      21-MAR-11
      1007 Robertson  25-MAR-11
        12 SCHNEIDER  20-MAR-11
         9 SPENCER    23-MAR-11
         3 TRICHLER   29-MAR-11
       918 VERREYNNE  14-MAR-11
        10 boehmer    22-MAR-11
        15 hall       17-MAR-11
       920 poder      12-MAR-11
         5 van wijk   27-MAR-11
      1021            11-MAR-11

21 rows selected.

SQL>

upper(name) 排序使其不区分大小写:

SQL> select id, name, somedate
  2  from t42
  3  order by upper(name), somedate
  4  /

        ID NAME       SOMEDATE
---------- ---------- ---------
         8 Billington 24-MAR-11
        10 boehmer    22-MAR-11
        13 Cave       19-MAR-11
         4 Clarke     28-MAR-11
       919 Feuerstein 13-MAR-11
        16 Gasparotto 16-MAR-11
        15 hall       17-MAR-11
         1 Kestelyn   31-MAR-11
       917 Kishore    15-MAR-11
      1014 KULASH     18-MAR-11
         2 Lira       30-MAR-11
         6 PADFIELD   26-MAR-11
       920 poder      12-MAR-11
        11 Rigby      21-MAR-11
      1007 Robertson  25-MAR-11
        12 SCHNEIDER  20-MAR-11
         9 SPENCER    23-MAR-11
         3 TRICHLER   29-MAR-11
         5 van wijk   27-MAR-11
       918 VERREYNNE  14-MAR-11
      1021            11-MAR-11

21 rows selected.

SQL>

CASE() 通过首先对指定 ID 范围内的所有记录进行分组,然后对所有其他记录进行分组,进一步更改了此设置。所选范围内的记录仅按日期排序,而其他记录仍按名称然后日期排序:

SQL> select id, name, somedate
  2  from t42
  3          ORDER BY
  4          (
  5              CASE
  6                  WHEN id BETWEEN 900 AND 999 THEN '1AAAAA'
  7                  ELSE '2'
  8                      || upper(name)
  9              END) ASC,
 10          somedate DESC
 11  /

        ID NAME       SOMEDATE
---------- ---------- ---------
       917 Kishore    15-MAR-11
       918 VERREYNNE  14-MAR-11
       919 Feuerstein 13-MAR-11
       920 poder      12-MAR-11
      1021            11-MAR-11
         8 Billington 24-MAR-11
        10 boehmer    22-MAR-11
        13 Cave       19-MAR-11
         4 Clarke     28-MAR-11
        16 Gasparotto 16-MAR-11
        15 hall       17-MAR-11
         1 Kestelyn   31-MAR-11
      1014 KULASH     18-MAR-11
         2 Lira       30-MAR-11
         6 PADFIELD   26-MAR-11
        11 Rigby      21-MAR-11
      1007 Robertson  25-MAR-11
        12 SCHNEIDER  20-MAR-11
         9 SPENCER    23-MAR-11
         3 TRICHLER   29-MAR-11
         5 van wijk   27-MAR-11

21 rows selected.

SQL>

Here is some data. As you can see the name sorts in ASCII order, which is not exactly the same as alphabetical order:

SQL> select id, name, somedate
  2  from t42
  3  order by name, somedate
  4  /

        ID NAME       SOMEDATE
---------- ---------- ---------
         8 Billington 24-MAR-11
        13 Cave       19-MAR-11
         4 Clarke     28-MAR-11
       919 Feuerstein 13-MAR-11
        16 Gasparotto 16-MAR-11
      1014 KULASH     18-MAR-11
         1 Kestelyn   31-MAR-11
       917 Kishore    15-MAR-11
         2 Lira       30-MAR-11
         6 PADFIELD   26-MAR-11
        11 Rigby      21-MAR-11
      1007 Robertson  25-MAR-11
        12 SCHNEIDER  20-MAR-11
         9 SPENCER    23-MAR-11
         3 TRICHLER   29-MAR-11
       918 VERREYNNE  14-MAR-11
        10 boehmer    22-MAR-11
        15 hall       17-MAR-11
       920 poder      12-MAR-11
         5 van wijk   27-MAR-11
      1021            11-MAR-11

21 rows selected.

SQL>

Sorting by upper(name) makes it case-insensitive:

SQL> select id, name, somedate
  2  from t42
  3  order by upper(name), somedate
  4  /

        ID NAME       SOMEDATE
---------- ---------- ---------
         8 Billington 24-MAR-11
        10 boehmer    22-MAR-11
        13 Cave       19-MAR-11
         4 Clarke     28-MAR-11
       919 Feuerstein 13-MAR-11
        16 Gasparotto 16-MAR-11
        15 hall       17-MAR-11
         1 Kestelyn   31-MAR-11
       917 Kishore    15-MAR-11
      1014 KULASH     18-MAR-11
         2 Lira       30-MAR-11
         6 PADFIELD   26-MAR-11
       920 poder      12-MAR-11
        11 Rigby      21-MAR-11
      1007 Robertson  25-MAR-11
        12 SCHNEIDER  20-MAR-11
         9 SPENCER    23-MAR-11
         3 TRICHLER   29-MAR-11
         5 van wijk   27-MAR-11
       918 VERREYNNE  14-MAR-11
      1021            11-MAR-11

21 rows selected.

SQL>

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> select id, name, somedate
  2  from t42
  3          ORDER BY
  4          (
  5              CASE
  6                  WHEN id BETWEEN 900 AND 999 THEN '1AAAAA'
  7                  ELSE '2'
  8                      || upper(name)
  9              END) ASC,
 10          somedate DESC
 11  /

        ID NAME       SOMEDATE
---------- ---------- ---------
       917 Kishore    15-MAR-11
       918 VERREYNNE  14-MAR-11
       919 Feuerstein 13-MAR-11
       920 poder      12-MAR-11
      1021            11-MAR-11
         8 Billington 24-MAR-11
        10 boehmer    22-MAR-11
        13 Cave       19-MAR-11
         4 Clarke     28-MAR-11
        16 Gasparotto 16-MAR-11
        15 hall       17-MAR-11
         1 Kestelyn   31-MAR-11
      1014 KULASH     18-MAR-11
         2 Lira       30-MAR-11
         6 PADFIELD   26-MAR-11
        11 Rigby      21-MAR-11
      1007 Robertson  25-MAR-11
        12 SCHNEIDER  20-MAR-11
         9 SPENCER    23-MAR-11
         3 TRICHLER   29-MAR-11
         5 van wijk   27-MAR-11

21 rows selected.

SQL>
べ繥欢鉨o。 2024-11-05 07:21:28

1.“1AAAAA”和“2”是什么意思?

那是文字常量。

2. || 的含义是什么upper(id.name)

|| 是 SQL 标准串联运算符。 'A' || “B”产生“AB”。

恕我直言,您的问题是整个 order by case 的含义,因此,请逐步进行:

             ORDER BY
                    (
                        CASE
                            WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
                            ELSE '2'
                                || upper(id.name)
                        END) ASC,
                    r.date DESC ;

这将按 case 表达式评估的结果(上升)对结果集进行排序,然后按 r.date (后裔)。

对于 900 到 999 之间的任何 ID,该案例将仅返回“1AAAAA”(这将按 r.date 排序,还记得吗?”

对于任何其他值,它将在 id.name 之前连接 2。

这可确保任何具有 id 的记录900 到 999 之间出现在第一个“组”中,该组仅按日期降序排列,然后第二组将包含所有其他记录,按名称的大写字母排序,然后按日期排序

。这些数据以了解其工作原理...只需将 case 表达式作为新列添加到您的 select 语句中,

例如,如果您的查询如下所示:

SELECT r.id, id.name
  FROM

添加这样的 case:

SELECT r.id, id.name
       , 
       CASE
         WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
         ELSE '2'|| upper(id.name)
       END ORDER_CRITERIA
  FROM

这将帮助您了解该表达式发生了什么。 ,因为您将看到生成的数据作为查询的最后一列。

1. what is the meaning of the '1AAAAA' and '2' ?

That are literal constants.

2. what is the meaning of || upper(id.name)

|| 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:

             ORDER BY
                    (
                        CASE
                            WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
                            ELSE '2'
                                || upper(id.name)
                        END) ASC,
                    r.date DESC ;

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:

SELECT r.id, id.name
  FROM

add the case like this:

SELECT r.id, id.name
       , 
       CASE
         WHEN r.id BETWEEN 900 AND 999 THEN '1AAAAA'
         ELSE '2'|| upper(id.name)
       END ORDER_CRITERIA
  FROM

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文