oracle sql问题98
我需要显示最新订单的日期以及与该订单关联的订单id,但我似乎不知道如何仅返回该最新订单的唯一订单id。 以下是我目前掌握的情况:oracle sql问题98,sql,oracle,greatest-n-per-group,Sql,Oracle,Greatest N Per Group,我需要显示最新订单的日期以及与该订单关联的订单id,但我似乎不知道如何仅返回该最新订单的唯一订单id。 以下是我目前掌握的情况: select custlastname || ', ' || custfirstname as Contact_Name, max(orderdate), orderid from customer inner join custorder on customer.customerid = custorder.customerid where state = 'OH
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;
这将返回10个结果,但我只想要具有该特定订单ID的最新订单?您可以使用
行号()执行此操作:
我以前从未用过/在课堂上学过。还有其他可能的方法吗?有很多种方法,但这可能是最好的方法。还有什么方法可以说明刚开始学习sql的人能够更好地与之关联?请避免在回答问题后大量编辑问题(删除相关信息)。如果稍后有人访问您的问题,那么这样做会让人非常困惑。
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;