排序和限制行 db官方例子

创建表

create table toys (

  toy_name       varchar2(30),

  weight         integer,

  price          number(5,2),

  purchased_date date,

  last_lost_date date

);

insert into toys values ('Miss Snuggles', 49.99date'2018-02-01', date'2018-06-01');

insert into toys values ('Baby Turtle',   15.00date'2016-09-01', date'2017-03-03');

insert into toys values ('Kangaroo',      10, 29.99, date'2017-03-01', date'2018-06-01');

insert into toys values ('Blue Dinosaur', 89.99date'2013-07-01', date'2016-11-01');

insert into toys values ('Purple Ninja'829.99, date'2018-02-01', null);

commit;

 

查看表

select * from toys;

排序

使用 order by排序。将数字从小到大排序

select * from toys

order  by price;

日期从最旧到最新排序

select * from toys

order  by purchased_date;

 字符数据按字母顺序排序

select * from toys

order  by toy_name;

倒序排序

select * from toys

order  by price desc;

对两列进行排序 

select toy_name, price from toys

order  by price, toy_name;

排序空

默认情况下,Oracle 数据库在数据中最后对 null 进行排序

select * from toys

order  by last_lost_date;

您可以使用 nulls 子句更改此设置。将空值在顶部显示

select * from toys

order  by last_lost_date nulls first;

自定义排序

按名称对玩具进列进行排序。但是Miss Snuggles是您的最爱,因此您希望它始终出现在顶部。

1 代表Miss Snuggles,2 代表其他

select * from toys

order  by case

  when toy_name = 'Miss Snuggles' then 1

  else 2

end, toy_name;

位置符号与别名

选择中包含表达式

有点蒙了

select t.*,

       case

         when toy_name = 'Miss Snuggles' then 1

         else 2

       end

from   toys t

order  by case

         when toy_name = 'Miss Snuggles' then 1

         else 2

       end, toy_name;

位置符号

select t.*,

       case

         when toy_name = 'Miss Snuggles' then 1

         else 2

       end

from   toys t

order  by 6, 1;

别名

给函数一个别名。然后在 order by 子句中引用这个别名

select t.*,

       case

         when toy_name = 'Miss Snuggles' then 1

         else 2

       end custom_sort

from   toys t

order  by custom_sort, toy_name;

N 个查询

Rownum 是特定于 Oracle 的函数。它为您获取的每一行分配一个递增的数字。

但是如果你在 order by 之前的 where 子句中使用它,你会得到意想不到的结果。例如,以下尝试获取三个最昂贵的玩具        (错误的)

select * from toys

where  rownum <= 3

order  by price desc;

在子查询中对数据进行排序        (价格排序)

select * from (

  select *

  from   toys t

  order  by price desc

)

where  rownum <= 3;

Row_number 是一个解析函数。与 rownum 一样,它分配一个递增计数器。这由 over 子句中的 order by 中定义的排序决定。

要在 top-N 查询中使用它,您还必须使用子查询:

select * from (

  select t.*, row_number() over (order by price desc) rn

  from   toys t

)

where  rn <= 3

order  by rn;

 

 12c 引入了符合 ANSI 的 fetch first 子句。这遵循 order by 并消除使用子查询的需要

select * from toys

order  by price desc

fetch  first 3 rows only;

前 N 名并列

select toy_name, price from toys

order  by price desc

fetch  first 3 rows with ties;

select * from (

  select t.*,

         rank() over ( order by price desc ) rn

  from   toys t

)

where  rn <= 3

order  by rn;

前三个值的所有行,请在子查询中使用dense_rank

select * from (

  select t.*,

         dense_rank() over ( order by price desc ) rn

  from   toys t

)

where  rn <= 3

order  by rn;

          

猜你喜欢

转载自blog.csdn.net/weixin_39568073/article/details/121188165