| Author |
Topic |
|
hardys
Starting Member
8 Posts |
Posted - 2009-08-21 : 11:37:19
|
empno ename job mgr hiredate salary dept commission7369 Smith Clerk 7902 1980-12-17 00:00:00.000 800.00 20 0.007499 Alen Salesman 7698 1981-02-20 00:00:00.000 1600.00 30 30.007521 Ward Salesman 7698 1981-02-22 00:00:00.000 1250.00 30 500.007566 John Manager 7839 1981-04-02 00:00:00.000 2975.00 20 0.007654 Martin Salesman 7698 1981-09-28 00:00:00.000 1250.00 30 1400.007698 Blake Manager 7839 1981-05-01 00:00:00.000 2850.00 30 0.007782 Clark Manager 7839 1981-06-09 00:00:00.000 2450.00 10 0.007788 Scott Analyst 7566 1987-04-19 00:00:00.000 3000.00 20 0.007839 King President 1981-11-17 00:00:00.000 5000.00 10 0.007844 Terner Salesman 7698 1981-09-08 00:00:00.000 1500.00 30 0.007876 Adams Clerk 7788 1981-05-23 00:00:00.000 1100.00 20 0.007900 James Clerk 7698 1981-12-03 00:00:00.000 950.00 30 0.007902 Fard Analyst 7566 1981-12-03 00:00:00.000 3000.00 20 0.007934 Miller Clerk 7782 1982-01-23 00:00:00.000 1300.00 10 0.00 this is the database ... i want to select the oldest employees from each work group.the querry i written was select job as [Job],ename as [Oldest Employee] from emp where hiredate in (select min(hiredate) from emp group by job)order by job;and the result isAnalyst FardClerk JamesClerk SmithManager JohnPresident KingSalesman Alenthe problem is that the clerk appears two time becoz james hiredate and fards hiredate are same.how can i get rid of that james other than using this querryselect job as [Job],ename as [Oldest Employee] from emp where hiredate in (select min(hiredate) from emp group by job) and ename <> 'james'order by job;please help anyone ... |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-08-21 : 11:45:01
|
| [code]select e.job as [Job], e.ename as [Oldest Employee] from emp ejoin (select job, min(hiredate) as hiredate from emp group by job) mon e.job = m.job and e.hiredate = m.hiredateorder by e.job;[/code] |
 |
|
|
hardys
Starting Member
8 Posts |
Posted - 2009-08-21 : 11:46:38
|
thanks a lot it worked .... thanks for the fast reply |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-08-21 : 11:48:25
|
| np, and you understand the difference in this and yours right? |
 |
|
|
hardys
Starting Member
8 Posts |
Posted - 2009-08-21 : 12:04:15
|
| yup .... thanks a lot ... hey 1 more doubt could u suggest me a better querry for finding the employee whos getting 4th largest salary the querry i wrote isselect top 1 ename as [Employee Having 4 th largest salary],sal as Salary from emp where sal in (select top 4 sal from emp order by sal desc) order by sal asc; |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-08-21 : 12:21:20
|
| what if the top 5 highly paid employee's have the same salary? what do you expect in the answer? |
 |
|
|
hardys
Starting Member
8 Posts |
Posted - 2009-08-21 : 12:37:00
|
| this is the resultJohn 2975.00the problem is to find the employee having 4 th largest salaryKing 5000.00Fard 3000.00Scott 3000.00John 2975.00Blake 2850.00Clark 2450.00Alen 1600.00Terner 1500.00Miller 1300.00Ward 1250.00Martin 1250.00Adams 1100.00James 950.00Smith 800.00here john is having the 4th largest salary ... i want to knw is there a better way 2 find it than through my querry |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-08-21 : 13:09:17
|
| [code]Select name,Salary from(Select name,Salary,ROW_NUMBER() Over (Partition by name order by salary desc)as ROWIDfrom Table)ZWhere Z.ROWID = 4[/code] |
 |
|
|
hardys
Starting Member
8 Posts |
Posted - 2009-08-21 : 13:37:59
|
Select ename,sal from(Select ename,sal,ROW_NUMBER() Over (Partition by ename order by sal desc)as ROWIDfrom emp)ZWhere Z.ROWID = 4;this doesnt return anything .. could u please explain whatz dis..when the inner querry is executed the rowid is always 1 here is the result for inner querrySelect ename,sal,ROW_NUMBER() Over (Partition by ename order by sal desc)as ROWIDfrom emp;ename sal rowidAdams 1100.00 1Alen 1600.00 1Blake 2850.00 1Clark 2450.00 1Fard 3000.00 1James 950.00 1John 2975.00 1King 5000.00 1Martin 1250.00 1Miller 1300.00 1Scott 3000.00 1Smith 800.00 1Terner 1500.00 1Ward 1250.00 1 whats the problem ? |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-08-21 : 13:57:25
|
quote: Originally posted by hardys this is the resultJohn 2975.00the problem is to find the employee having 4 th largest salaryKing 5000.00Fard 3000.00Scott 3000.00John 2975.00Blake 2850.00Clark 2450.00Alen 1600.00Terner 1500.00Miller 1300.00Ward 1250.00Martin 1250.00Adams 1100.00James 950.00Smith 800.00here john is having the 4th largest salary ... i want to knw is there a better way 2 find it than through my querry
you didn't answer my question. what should be the answer here??KingA 5000.00KingB 5000.00KingC 5000.00KingD 5000.00KingE 5000.00Fard 3000.00Scott 3000.00John 2975.00Blake 2850.00Clark 2450.00Alen 1600.00Terner 1500.00Miller 1300.00Ward 1250.00Martin 1250.00Adams 1100.00James 950.00Smith 800.00 |
 |
|
|
hardys
Starting Member
8 Posts |
Posted - 2009-08-21 : 14:34:24
|
| the result must be john 2975.00my querry just returns kingd ... |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-08-21 : 14:36:28
|
You need this...Select name,Salary from(Select name,Salary,ROW_NUMBER() Over (order by salary desc)as ROWIDfrom Table)ZWhere Z.ROWID = 4 but as Rohit said, it might not give you expected results if people have the same salaries. You need to tell us if thats the case.EDIT: Just saw your reply...you need this I think..Select name,Salary from(Select name,Salary,DENSE_RANK() Over (order by salary desc)as ROWIDfrom Table)ZWhere Z.ROWID = 4 |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-08-21 : 14:41:42
|
See my edited post...you need thisSelect name,Salary from(Select name,Salary,DENSE_RANK() Over (order by salary desc)as ROWIDfrom Table)ZWhere Z.ROWID = 4 |
 |
|
|
hardys
Starting Member
8 Posts |
Posted - 2009-08-21 : 14:43:42
|
| yup its workin ... could u please explain it ... thanks a lot |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-08-21 : 14:45:39
|
| Check this..http://blogs.msdn.com/craigfr/archive/2008/03/31/ranking-functions-rank-dense-rank-and-ntile.aspx |
 |
|
|
|