Oracle sql 发现发货延迟

Oracle sql fiding shipping delay

我能知道如何编写 sql 语句来列出发货延迟时间最长的订单的发货城市和州吗?我不确定是否需要在

中使用函数 Max

测试table:

create table orders
(id int
,state varchar2(30)
,city varchar2(30)
,delay int);

测试数据:

insert into orders(id, state, city, delay)
select rownum as id, owner as state, table_name as city, abs(dbms_random.random) as delay
  from all_tables;

查询:

select * from (
select o.*, row_number() over (order by delay desc) pos from orders o
) where pos = 1

我假设您的数据结构是扁平的,如:

CREATE TABLE orders (
  ...
  shipping_city VARCHAR2(100),
  shipping_state VARCHAR2(100),
  shipping_delay NUMBER(7),
  ...
);

您现在可以写:

SELECT
  MAX(shipping_city)  KEEP (DENSE_RANK LAST ORDER BY shipping_delay),
  MAX(shipping_state) KEEP (DENSE_RANK LAST ORDER BY shipping_delay)
FROM
  orders