0

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 Answer 1

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;
3
  • Thank you so much again A.B.Cade. Your answer is so genius likes you're sitting next to me even you didn't see any data. I will change my religious status into you now. Thank you May 19, 2013 at 16:34
  • excuse me A.B.Cade, but where can I add rank function over here. It keeps error. Should I use rank() with over() function? May 20, 2013 at 12:02
  • @NiickMosqow, this is pretty much like your other question... There is a nice post here which I think you need to read...
    – A.B.Cade
    May 20, 2013 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.