【问题标题】:Order by statement - ordering by multiple columns按语句排序 - 按多列排序
【发布时间】:2016-08-15 14:00:15
【问题描述】:

我有这样的表:

A       B       C
-----------------------
111     3
                777
333     1
555     2
                333
777     4
888     5

所以,我有 order by 语句“order by B”,结果如下:

A       B       C
----------------------
333     1
555     2
111     3
777     4
888     5
                777
                333

但是,我该怎么做才能得到这种排序:

A       B       C
-----------------------
333     1
                333
555     2
111     3
777     4
                777
888     5

如果 C 列不为空,我应该将此行放在 A = C 的行之后

谢谢!

所以,在这种情况下:

with a(a,b,c) as (select 111,4, null from dual union all
                  select null,null,777 from dual union all
                  select 333,1,null  from dual union all
                  select 555,2, null from dual union all
                  select null,null, 333 from dual union all
                  select 777, 4, null from dual union all

                  select 444,null, 333 from dual union all

                  select 888, 5, null from dual union all
                  select null,null,777 from dual )

select a.*
  from a
 order by last_value(b ignore nulls) 
       over (partition by CASE when b is null then c else a end order by b), b nulls last

我有那个输出(C 777 在 A 111 之后,因为 B 值相同 = 4):

A    B     C
--------------------           
333     1   
444         333
            333
555     2   
777     4   
111     4   
            777
            777
888     5   

但我想得到这个:

    A    B     C
    --------------------
    333     1   
    444         333
                333
    555     2   
    777     4   
                777
                777
    111     4   
    888     5   

【问题讨论】:

  • 你能有A和C都不为空的行吗?
  • 是的,我可以拥有那几行
  • @user2783755,请检查我的答案,有 2 个变体

标签: sql oracle sql-order-by


【解决方案1】:

也许对你有帮助:

with a(a,b,c) as (select 111,3, null from dual union all
                  select null,null,777 from dual union all
                  select 333,1,null  from dual union all
                  select 555,2, null from dual union all
                  select null,null, 333 from dual union all
                  select 777, 4, null from dual union all
                  select 888, 5, null from dual )

select a.* 
  from a
 order by last_value(b ignore nulls) over (partition by nvl(a,c) order by b), b nulls last

输出

333 1   
        333
555 2   
111 3   
777 4   
        777
888 5   

已选择 7 行

或者正如你稍后所说,你可以同时拥有非空的 A 和 C 列,你可以这样做:

with a(a,b,c) as (select 111,3, null from dual union all
                  select null,null,777 from dual union all
                  select 333,1,null  from dual union all
                  select 555,2, null from dual union all
                  select null,null, 333 from dual union all
                  select 777, 4, null from dual union all

                  select 444,null, 333 from dual union all

                  select 888, 5, null from dual )

select a.*
  from a
 order by last_value(b ignore nulls) 
       over (partition by CASE when b is null then c else a end order by b), b nulls last

输出

         A          B          C
       333          1            
                             333 
       444                   333 
       555          2            
       111          3            
       777          4            
                             777 
       888          5            

 8 rows selected 

【讨论】:

  • 我还有一个问题。如果在 B 列中可以是相同的值,例如所有值都是 2(其中 A 不为空),那么我想要相同的排序。谢谢!
  • 在您的附加案例中您想要的顺序逻辑是什么?如果 b 相同,请给我数据示例并输出您想要的内容
  • 我已将此添加到问题中。感谢您的帮助。
  • @user2783755,我无法得到您需要的确切信息,但您可以通过订购 order by last_value(b ignore nulls) over (partition by CASE when b is null then c else a end), nvl(a,c) nulls last, c nulls last 之类的东西来进行更改
【解决方案2】:

这样做:

select case when C in not null then C else A end as A,B,C from table

类似这样:

Declare @c nchar(50)
Declare @a nchar(50)
set @c = 'record' 
set @a = 'sample'
select case  when  @c is not null then @c else @a end as A,@c as C

【讨论】:

  • when C in not null 是无效的 SQL。 declare @c 对 Oracle 也无效
  • @reds 是 Sql 但 rdbms 是 Oracle。
  • 尝试使用 if len(feilds)>=0 之类的函数
  • Oracle 使用 SQL。 “SQL”是所有关系型数据库都使用的查询语言
  • 我这么认为@a_horse_with_no_name
猜你喜欢
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2018-04-29
相关资源
最近更新 更多