0

i have a requirement, i need to find max only one val from each department,i need only one value even 2 person have same max value

drop table tst;
create table tst(val number,dept varchar2(20),name varchar2(10));
insert into tst values(1,'tamil','john');
insert into tst values(2,'tamil','krish');
insert into tst values(3,'maths','vijay');
insert into tst values(4,'maths','raja');
insert into tst values(4,'maths','vinay');

select * from tst;
VAL  DEPT   NAME
1   tamil   john
2   tamil   krish
3   maths   vijay
4   maths   raja
4   maths   vinay

when i tried to find max i will get 2 value for maths dept

select * from tst t1
where t1.val= (select max(val) from tst t2 where t2.dept=t1.dept
              group by dept);

2 tamil krish
4 maths raja
4 maths vinay

i want either

2 tamil krish
4 maths vinay

or

2 tamil krish
4 maths raja

how to achieve this ion oracle sql

2 Answers 2

1

You can use the ROW_NUMBER as follows:

SELECT * FROM
(SELECT T.*, ROW_NUMBER(OVER PARTITION BY DEPT ORDER BY VAL DESC) AS RN
   FROM TST T)
WHERE RN = 1
Sign up to request clarification or add additional context in comments.

Comments

1

One way is to add name too in an aggregate function

select max(VAL) val, dept, max(name) name
from tst
group by dept

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.