List By Biggest_Purchase In Desc Order In Group By Query

时间:2013-04-21 19:57:30

标签: sql sql-server-2008

以下是我的疑问:

select MAX(o.amount)as Biggest_Purchase , c.cust_fname, c.cust_lname 
from orders o, customer c
where o.cust_num = c.cust_num
group by c.cust_fname, c.cust_lname
order by o.amount desc;

为什么我收到以下错误?:

Msg 8127,Level 16,State 1,Line 5 ORDER BY子句中的“orders.amount”列无效,因为它不包含在聚合函数或GROUP BY子句中。

2 个答案:

答案 0 :(得分:1)

您只能在select的{​​{1}}子句中指定列。我想你的意思是:

order by

答案 1 :(得分:0)

select MAX(o.amount)as Biggest_Purchase , c.cust_fname, c.cust_lname 
from orders o, customer c
where o.cust_num = c.cust_num
group by c.cust_fname, c.cust_lname
order by max(o.amount) desc;