Return 最新日期的文章,sql max(DATE)

Return the article with the latest date, sql max(DATE)

我有以下查询:

 SELECT  MAX(b.upd_dtime) as MaxT, b.vo_no as vo_no, y.item_no
 FROM vo_order_t b JOIN (
     SELECT a.vo_no, a.item_no FROM vo_item_t a where a.item_no IN('00265929')) y ON y.vo_no = b.vo_no
 GROUP BY b.vo_no, y.item_no

此查询的输出如下:

Date                Vo_No   Item_No
2019-05-27 08:37:07 0242625 00265929
2019-05-27 07:52:29 0282971 00265929
2019-05-27 07:52:29 0282972 00265929
2019-05-27 07:52:29 0696864 00265929
2018-02-13 22:57:09 0282984 00265929
2019-05-27 07:52:29 0395347 00265929
2019-05-27 07:52:29 0242712 00265929
2019-05-27 07:52:29 0242624 00265929
2019-05-27 07:52:29 0441449 00265929
2019-05-27 07:52:29 0400026 00265929

但我希望输出如下:

Date                Vo_no   Item_No
2019-05-27 08:37:07 0242625 00265929

如何修改我的查询来实现?

分析函数可能会有所帮助。您需要的代码从第 6 行开始。

SQL> with test (upd_dtime, vo_no, item_no) as
  2    (select 20190527, 242625, 265929 from dual union all
  3     select 20190213, 282984, 265929 from dual union all
  4     select 20190118, 400026, 265929 from dual
  5    )
  6  select upd_dtime, vo_no, item_no
  7  from (
  8         select upd_dtime, vo_no, item_no,
  9           row_number() over (partition by item_no order by upd_dtime desc) rn
 10         from test
 11       )
 12  where rn = 1;

 UPD_DTIME      VO_NO    ITEM_NO
---------- ---------- ----------
  20190527     242625     265929

SQL>

[编辑:应用于您的表格]

因为您无法将上面的代码调整到您的真实表中(下次,最好是 post 测试用例,其中包括 CREATE TABLE 和 INSERT INTO 示例数据) ,它可能是这样的:

  • yourq CTE 表示您 post 编辑的查询。我不知道你为什么使用内联视图(它的别名是 y);你为什么不像我一样简单地加入 vo_order_tvo_item_t
  • 其余的和我之前post编辑的一模一样

因此:copy/paste 此代码并在您的模式中执行它。如果我没有打错字,应该没问题。如果不是,正如我所说 - post 测试用例。

with 
yourq as
-- your query, rewritten
  (select b.upd_dtime, b.vo_no, a.item_no
   from vo_order_t b join vo_item_t a on a.vo_no = b.vo_no
   where a.item_no = '00265929'
  )
select upd_dtime, vo_no, item_no
from (
      select upd_dtime, vo_no, item_no,
             row_number() over (partition by item_no order by upd_dtime desc) rn
      from yourq
     )
where rn = 1; 

您可以使用聚合的 keep 子句 return first/last 值,由另一个排序:

create table t (
  c1 date, c2 varchar2(10), c3 varchar2(10)
);
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';

insert into t values ( '2019-05-27 08:37:07', '0242625', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0282971', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0282972', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0696864', '00265929' );
insert into t values ( '2018-02-13 22:57:09', '0282984', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0395347', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0242712', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0242624', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0441449', '00265929' );
insert into t values ( '2019-05-27 07:52:29', '0400026', '00265929' );

select max ( c1 ) c1,
       max ( c2 ) keep (
         dense_rank first
         order by c1 desc
       ) c2,
       c3
from   t
group  by c3;

C1                    C2        C3         
2019-05-27 08:37:07   0242625   00265929   

可以使用MAX() OVER (PARTITION BY ...)解析函数:

WITH t AS
(   
 SELECT MAX(v.upd_dtime) OVER (PARTITION BY v.item_no) AS MaxT, 
        v.vo_no AS vo_no, v.item_no, v.upd_dtime 
   FROM vo_order_t v
  WHERE v.item_no = '00265929'
)
SELECT *
  FROM t
 WHERE upd_dtime = MaxT   

如果你使用的是12c+版本,那么直接使用FETCH FIRST 1 ROW ONLY获取最新文章:

 SELECT MAX(v.upd_dtime) OVER (PARTITION BY v.item_no) AS MaxT, 
        v.vo_no AS vo_no, v.item_no, v.upd_dtime 
   FROM vo_order_t v
  WHERE v.item_no = '00265929'
  FETCH FIRST 1 ROW ONLY

Demo