甲骨文 sql 问题 98
oracle sql question98
我需要显示最近订单的日期以及与该订单关联的订单 ID,但我似乎无法弄清楚如何只 return 最近订单的唯一订单 ID命令。
这是我目前所拥有的:
select custlastname || ', ' || custfirstname as Contact_Name,
max(orderdate), orderid
from customer inner join custorder
on customer.customerid = custorder.customerid
where state = 'OH' and companyname is not null
group by custlastname, custfirstname, orderid
order by custlastname, custfirstname;
这 return 有 10 个结果,但我只想要具有该特定订单 ID 的最新订单?
您可以使用 row_number()
:
select custlastname || ', ' || custfirstname as Contact_Name, orderid
from customer c inner join
(select o.*,
row_number() over (partition by customerid order by orderdate desc) as seqnum
from custorder o
) o
on c.customerid = o.customerid and seqnum = 1
where state = 'OH' and companyname is not null ;
order by custlastname, custfirstname;
我需要显示最近订单的日期以及与该订单关联的订单 ID,但我似乎无法弄清楚如何只 return 最近订单的唯一订单 ID命令。 这是我目前所拥有的:
select custlastname || ', ' || custfirstname as Contact_Name,
max(orderdate), orderid
from customer inner join custorder
on customer.customerid = custorder.customerid
where state = 'OH' and companyname is not null
group by custlastname, custfirstname, orderid
order by custlastname, custfirstname;
这 return 有 10 个结果,但我只想要具有该特定订单 ID 的最新订单?
您可以使用 row_number()
:
select custlastname || ', ' || custfirstname as Contact_Name, orderid
from customer c inner join
(select o.*,
row_number() over (partition by customerid order by orderdate desc) as seqnum
from custorder o
) o
on c.customerid = o.customerid and seqnum = 1
where state = 'OH' and companyname is not null ;
order by custlastname, custfirstname;