0

hi,
i have 2 tables: CUST and ORDERS
CUST contains cust_num, cust_name, cust_phone
ORDERS contains cust_num, order_date, order_amt
i want to list all the cust_names and their last order date and amount
how would i code this?
thanks.

9
Contributors
14
Replies
26
Views
10 Years
Discussion Span
Last Post by QVeen72
0

i want to list all the customer names and then latest date and amount for that date for every customer.

1

here's the correct answer that i figured out:
SELECT cu.cust_name,
od.order_amount
FROM customers cu,
orders od
WHERE cu.cust_id = od.cust_id
and od.order_date =
(select max(order_date) from orders od
where od.cust_id = cu.cust_id)

0

here's the correct answer that i figured out:
SELECT cu.cust_name,
od.order_amount
FROM customers cu,
orders od
WHERE cu.cust_id = od.cust_id
and od.order_date =
(select max(order_date) from orders od
where od.cust_id = cu.cust_id)

-1

here's the correct answer that i figured out:

SELECT cu.cust_name,
            od.order_amount
FROM customers cu, 
         orders od
WHERE cu.cust_id = od.cust_id 
  and od.order_date =
 (select max(order_date) from orders od
        where od.cust_id = cu.cust_id)

That's not a great way to do it, because you've got a subquery. Doing two queries when only one is needed.

You'll find it should perform faster if you do a join and then get the max all in one queery, something like below. It depends on how Oracle optimises it though, performance issues can be fun to play with. In general the following will be better than what you've got above:

select cu.cust_name, od.order_amount, max(order_date) 
  from customers cu
  join  orders od
    on (cu.cust_id = od.cust_id)
group by cu.cust_name, od.order_amount

Having said that, it really does depend on a number of factors which I won't get into for now.... anyway you have some options now ;)

Votes + Comments
faster but not what alidabiri want as answer
0

In query, we are selecting the date as max date using max function but suppose if the last inserted row is the date less than the existing dates [it means this is the last record we have inserted in table] & if we want to retrieve this row...then max function would fail.
In this scenario, how to retrieve the last row?

0

The query would be :
[this is just a sample query]

select empno,sal
from(select a.*, rownum rwnm from(select empno,sal from test 
                                                   order by rownum desc)a
       where rownum = 1)
-1

To view the last record of a table in oracle:-----


select * from (select regn_dt,rownum r, regn_no from vt_owner) where r >(select max(rownum) -4 from vt_owner);:icon_neutral:

Votes + Comments
read question properly before replying.
0

sql query for retrieving last row particular column value depending upon its ID???

QUERY:
select top 1 [column_name1],[column_name2] from [table_name] where ID=[ID_value] order by index desc

here index is a column name which contain the number of row in the table..
the above Query help you to retrieve last row in case when you don't the index contain value.

0

In my query, we are picking the latest record using lat_upd date column using using max function and its wrking fine but i got another problem like if the last_upd is same for multiple records its picking those 2 records..but i dont want to pick those two records.. i need to pick either one record if the last_upd date is same for multiple records.
Can any one suggest me on this ?

0

Hi,

Combined with lat_upd, You can Select using Order By RowID.. RowID is unique for each row...

Regards
Veena

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.