基于2列Oracle对结果进行排序

发布于 2025-01-17 14:45:32 字数 399 浏览 3 评论 0原文

我有两列“日期”和“数字”,日期中包含空值。我可以用日期和数字 col 一起排序,检查日期是否为空,然后用数字排序。

dt             num
3/20/2022       1
3/16/2022       3
3/17/2022       4
3/18/2022       5
NULL            6
NULL            7
3/19/2022       8

*Expected Output*
dt             num
3/16/2022       3
3/17/2022       4
3/18/2022       5
NULL            6
NULL            7
3/19/2022       8
3/20/2022       1

I have two columns Date and Number with null values in date. Can I sort with date and number col together checking if date is null then sort with number.

dt             num
3/20/2022       1
3/16/2022       3
3/17/2022       4
3/18/2022       5
NULL            6
NULL            7
3/19/2022       8

*Expected Output*
dt             num
3/16/2022       3
3/17/2022       4
3/18/2022       5
NULL            6
NULL            7
3/19/2022       8
3/20/2022       1

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

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

发布评论

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

评论(1

云仙小弟 2025-01-24 14:45:32

如果有,我们需要按日期排序,如果没有,我们搜索日期不为空的上一行。
这确实意味着我们每行运行一个子查询,因此对于大型查询来说会很慢。

创建表 d(
日期,
数字整数);
插入 d(dt, num)
从双重联合全部中选择 to_date('2022-03-20','YYYY-MM-DD'),1
从双重联合全部中选择 to_date('2022-03-16','YYYY-MM-DD'),3
select to_date ('2022-03-17','YYYY-MM-DD'), 4 from Dual Union all
从双重联合全部中选择 to_date('2022-03-17','YYYY-MM-DD'),5
从双重联合全部中选择 to_date('2022-03-18','YYYY-MM-DD'),6
从双重联合全部中选择 to_date('2022-03-16','YYYY-MM-DD'),10
从对偶中选择 to_date('2022-03-19','YYYY-MM-DD'),9; 
插入到 d (num)
从双并全部中选择 7
从 Dual 中选择 8;

<前><代码>选择
德特,
编号,
(选择dt
从 d
其中 num <= d1.num 并且 dt 不为空
按 num desc 排序
仅获取接下来的 1 行
) 作为 dt_plus
从 d d1
按 dt_plus,num 排序;

<前>DT |编号 | DT_PLUS
:-------- | --: | :--------
22 年 3 月 16 日 | 3 | 22 年 3 月 16 日
22 年 3 月 16 日 | 10 | 10 22 年 3 月 16 日
22 年 3 月 17 日 | 4 | 22 年 3 月 17 日
22 年 3 月 17 日 | 5 | 22 年 3 月 17 日
22 年 3 月 18 日 | 6 | 22 年 3 月 18 日
| 7 | 22 年 3 月 18 日
| 8 | 22 年 3 月 18 日
22 年 3 月 19 日 | 9 | 22 年 3 月 19 日
22 年 3 月 20 日 | 1 | 22 年 3 月 20 日

db<>fiddle 此处

We need to sort by the date if there is one and if there is not we search the previous row where the date is not null.
This does mean that we are running a sub-query per line so it will be slow for large queries.

create table d(
dt date,
num int);
insert into d (dt, num)
select to_date('2022-03-20','YYYY-MM-DD'),1 from dual union all
select to_date('2022-03-16','YYYY-MM-DD'),3 from dual union all
select to_date ('2022-03-17','YYYY-MM-DD'), 4 from dual union all
select  to_date('2022-03-17','YYYY-MM-DD'),5 from dual union all
select  to_date('2022-03-18','YYYY-MM-DD'),6 from dual union all
select  to_date('2022-03-16','YYYY-MM-DD'),10 from dual union all
select  to_date('2022-03-19','YYYY-MM-DD'),9 from dual; 
insert into d ( num)
select     7 from dual union all
select     8 from dual ;
select 
  dt,
  num,
  ( select dt 
    from d 
    where num <= d1.num and dt is not null 
    order by num desc 
    fetch next 1 rows only 
  ) as dt_plus
from d d1
order by dt_plus,num;
DT        | NUM | DT_PLUS  
:-------- | --: | :--------
16-MAR-22 |   3 | 16-MAR-22
16-MAR-22 |  10 | 16-MAR-22
17-MAR-22 |   4 | 17-MAR-22
17-MAR-22 |   5 | 17-MAR-22
18-MAR-22 |   6 | 18-MAR-22
null      |   7 | 18-MAR-22
null      |   8 | 18-MAR-22
19-MAR-22 |   9 | 19-MAR-22
20-MAR-22 |   1 | 20-MAR-22

db<>fiddle here

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