sql选择最新的订单日期

时间:2016-06-02 17:54:51

标签: sql oracle

我想检索订购它的客户的最新订单和名称:

Select fname, lname, orderdate as newestorder
from customer, cusorder
where customer.cid = cusorder.cid
group by fname, lname
having orderdate = max(orderdate)
;

错误是ORA-00979:不是GROUP BY表达式

3 个答案:

答案 0 :(得分:3)

您收到错误是因为您在选择中包含了Select fname, lname, orderdate as newestorder from customer inner join cusorder on customer.cid = cusorder.cid where orderdate = (select max(orderdate) from cusorder) ,但您没有对其进行分组。

那说它不会做你想做的事。有很多方法可以做你想要的。这是一个

private String getInput() {
    return new Scanner(System.in).nextLine();
}

答案 1 :(得分:0)

尝试使用连接insted点。这被认为是良好的做法。试试这个:

Select 

fname, lname , max(orderdate) as newestorder
from customer AS c
INNER JOIN cusorder as cu on c.cid = cu.cid
group by fname, lname
order by newestorder desc
;

答案 2 :(得分:0)

假设名称相同,

应该能够在select查询上执行最大日期。

Select fname, lname, max(orderdate) as newestorder
from customer, cusorder
where customer.cid = cusorder.cid
group by fname, lname