简体   繁体   English

Oracle sql 先排序较大的日期,基于2个日期列

[英]Oracle sql sort greater date first, based on 2 date column

I have table which contains 2 columns CREATION_DATE AND CHANGE_DATE.我有一个包含 2 列 CREATION_DATE 和 CHANGE_DATE 的表。 I am looking for a way to sort data based on date which is greater.我正在寻找一种基于更大日期对数据进行排序的方法。

May be like:可能是这样的:

select * from table order by compare(CREATION_DATE ,CHANGE_DATE) desc;

the compare(CREATION_DATE,CHANGE_DATE) function suppose to return greater date between both columns. compare(CREATION_DATE,CHANGE_DATE) function 假设在两列之间返回更大的日期。

I have search a lot and tried various combination with date function and NULLS FIST, LAST but not use.我进行了很多搜索,并尝试了各种日期 function 和 NULLS FIST,LAST 但不使用的组合。

any help is greatly appreciated.任何帮助是极大的赞赏。

If neither date is NULL , then you can use:如果两个日期都不是NULL ,那么您可以使用:

order by greatest(creation_date, change_date) desc

Unfortunately, greatest() returns NULL if any value is NULL .不幸的是,如果任何值为NULLgreatest()返回NULL

If change_date is either NULL or bigger than creation_date , then you can use coalesce() :如果change_dateNULL或大于creation_date ,那么您可以使用coalesce()

order by coalesce(change_date, creation_date)

This situation makes sense to me -- changing something before it is created is unusual.这种情况对我来说很有意义——在创建之前更改某些东西是不寻常的。

If change_date can be NULL and can also be less than creation_date , then you need to be more careful.如果change_date可以是NULL并且也可以小于creation_date ,那么你需要更加小心。 You need to replace by a value.您需要用一个值替换。 Presumably, creation_date would not be NULL and you can use coalesce() :据推测, creation_date不会是NULL并且您可以使用coalesce()

order by greatest(creation_date, coalesce(change_date, creation_date)) desc

Or a case expression:case表达式:

order by (case when change_date > creation_date then change_date
               else creation_date
          end)

There's no compare , but - use greatest .没有compare ,但是 - 使用greatest的。

order by greatest(creation_date, change_date) desc

Do you mean this?你是这个意思吗? You can order by Boolean expressions...您可以通过 Boolean 表达式订购...

WITH
indata(comp,cr_dt,chg_dt) AS (
          SELECT 'same'      ,'2021-02-15', '2021-02-15' FROM dual
UNION ALL SELECT 'cr_bigger' ,'2021-02-16', '2021-02-15' FROM dual
UNION ALL SELECT 'chg_bigger','2021-02-15', '2021-02-16' FROM dual
UNION ALL SELECT 'cr_bigger' ,'2021-02-26', '2021-02-25' FROM dual
UNION ALL SELECT 'chg_bigger','2021-02-25', '2021-02-26' FROM dual
)
SELECT
  CASE WHEN cr_dt > chg_dt
    THEN 1
    ELSE 0
  END AS cr_is_greater
, comp
, cr_dt
, chg_dt
FROM indata
ORDER BY 1 DESC
;
 cr_is_greater |    comp    |   cr_dt    |   chg_dt
---------------+------------+------------+------------
             1 | cr_bigger  | 2021-02-16 | 2021-02-15
             1 | cr_bigger  | 2021-02-26 | 2021-02-25
             0 | same       | 2021-02-15 | 2021-02-15
             0 | chg_bigger | 2021-02-15 | 2021-02-16
             0 | chg_bigger | 2021-02-25 | 2021-02-26

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM