0

对于 Oracle,从这些表中,

表格1CUSTOMER : has cust_fname, cust_lname, cust_id

表 2SALESORDER : has so_number, so_custid

表3ITEM : has item_qty, item_sonum

while CUST_ID = SO_CUSTID and SO_NUMBER = ITEM_SONUM (FK relationship)

我想用全名显示客户名称(表示 cust_fname+cust_lname),而这个客户(只有一个)是订购数量最多的客户(表示必须对 item_qty 做某事)。

我怎样才能为这个任务编写代码?

谢谢

4

1 回答 1

1

尝试

select cust_fname, cust_lname
from
(select c.cust_fname, c.cust_lname
from customer c join salesorder so on so.so_custid = c.cust_id
join ITEM i on i.item_sonum = so.so_number
group by c.cust_fname, cust_lname
order by sum(i.item_qty) desc)
where rownum = 1

这是一个 sqlfiddle 演示

select cust_fname, cust_lname
from
(select c.cust_fname, c.cust_lname, rank() over (order by sum(i.item_qty) desc) rnk
from customer c join salesorder so on so.so_custid = c.cust_id
join ITEM i on i.item_sonum = so.so_number
group by c.cust_fname, cust_lname
)
where rnk = 1;
于 2013-05-19T16:19:32.887 回答