Q) How to Find Max Date from each Group? (Asked in Infosys
(INFI)Interview)
Answer Posted / niladri chatterjee
SQL> Select * From Market;
MARKET_ID MKT_NAME AREA SALE_DT
---------------------- -------- ---- ----------
1 uss NE 25-JAN-12
1 uss NE 24-FEB-12
1 uss NE 20-JUN-11
1 uss NE 15-MAR-11
2 rus SE 21-MAR-11
2 rus NE 24-APR-11
3 ger SE 20-FEB-11
3 ger NE 22-MAR-11
3 ger NE 24-FEB-12
My Answers:-
For the Single Max Row:
Select * From (Select * From market Order By Sale_Dt Desc)
Where rownum = 1;
Followings are for each Groups:
Select *
from market a
where a.sale_dt =
(select max(b.sale_dt) from market b
where a.market_id = b.market_id);
OR
select market_id, mkt_name, max(sale_dt)
from market
group by market_id, mkt_name;
| Is This Answer Correct ? | 9 Yes | 1 No |
Post New Answer View All Answers
What is the relation of a user account and a schema?
You have found corruption in a tablespace that contains static tables that are part of a database that is in NOARCHIVE log mode. How would you restore the tablespace without losing new data in the other tablespaces?
What is a nested table and how is it different from a normal table?
What do you mean by merge in oracle?
What is Library Cache in Oracle?
How to rename a column in an existing table?
Is there a combination of "like" and "in" in sql?
what is the use of system.effective.date variable in oracle?
how to handle exceptions in post production
How to select some columns from a table in oracle?
How to Truncate Table in Oracle
How to initialize variables with default values?
Please explain drop constraint oracle?
Explain oracle data types with examples?
How many file formats are supported to export data?