Inception situation with oracle (sub query stuffs)

岁酱吖の 提交于 2019-12-12 02:57:54

问题


For Oracle, From these tables,

Table 1 CUSTOMER : has cust_fname, cust_lname, cust_id

Table 2 SALESORDER : has so_number, so_custid

Table 3 ITEM : has item_qty, item_sonum

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

I want to show customer name with full name (means cust_fname+cust_lname) whereas this customer (just one) is the one who had ordered the highest numbers of items (means has to do something with item_qty).

How can I write a code for this task?

Thank you


回答1:


Try

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

Here is a sqlfiddle demo

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;


来源:https://stackoverflow.com/questions/16636487/inception-situation-with-oracle-sub-query-stuffs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!