Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
khurrat
Starting Member
6 Posts |
Posted - 2006-11-10 : 04:01:43
|
i have a table--- dept with 3 columns.. empid,deptid,sal1 11 1112 11 2223 11 3334 22 1005 22 2006 22 3007 33 10008 33 20009 33 3000i want to select the employee with second highest salary in EACH GROUP!!..could some one help me out with this one...should not use a cursor...only select statements |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 04:14:49
|
You mean for each department?-- prepare test datadeclare @test table (empid tinyint, deptid tinyint, sal smallint)insert @testselect 1, 11, 111 union allselect 2, 11, 222 union allselect 3, 11, 333 union allselect 4, 22, 100 union allselect 5, 22, 200 union allselect 6, 22, 300 union allselect 7, 33, 1000 union allselect 8, 33, 2000 union allselect 9, 33, 3000-- perform the magicselect t.*from @test tinner join ( select deptid, max(sal) ms from @test group by deptid ) q on q.deptid = t.deptid and q.ms = t.salorder by t.sal desc Why are you asking us to do your homework for you?Peter LarssonHelsingborg, Sweden |
 |
|
khurrat
Starting Member
6 Posts |
Posted - 2006-11-10 : 04:44:40
|
Thanks for ur response peter,there are many ways of getting a max sal for each department.BUT I am interested in getting second highest, the result should be this:2 11 2225 22 2008 33 2000This must be simple query for you, but m in a learning phase.I have a query which gives second highest value:how do i use this to get the same for each deptidselect max(sal) from dept where sal not in (select top 1 sal from dept order by sal desc)Thank you. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 05:02:08
|
Try this-- prepare test datadeclare @test table (empid tinyint, deptid tinyint, sal smallint)insert @testselect 1, 11, 111 union allselect 2, 11, 222 union allselect 3, 11, 333 union allselect 4, 22, 100 union allselect 5, 22, 200 union allselect 6, 22, 300 union allselect 7, 33, 1000 union allselect 8, 33, 2000 union allselect 9, 33, 3000-- perform the magicselect t.*from @test tinner join ( select deptid, sum(sal) - max(sal) - min(sal) ms from @test group by deptid ) q on q.deptid = t.deptid and q.ms = t.salorder by t.sal desc since it obviously is either an interview question or homework.Peter LarssonHelsingborg, Sweden |
 |
|
khurrat
Starting Member
6 Posts |
Posted - 2006-11-10 : 05:16:46
|
cool!!... but , what if I have many rows for each group??, not jus 3 . |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 05:28:16
|
Yikes.-- prepare test datadeclare @test table (empid tinyint, deptid tinyint, sal smallint)insert @testselect 1, 11, 111 union allselect 2, 11, 222 union allselect 3, 11, 333 union allselect 4, 22, 100 union allselect 5, 22, 200 union allselect 6, 22, 300 union allselect 7, 33, 1000 union allselect 8, 33, 2000 union allselect 9, 33, 3000-- perform the magicselect t.*from ( select deptid, max(sal) ms from @test group by deptid ) w inner join @test t on t.deptid = w.deptid and t.sal = (select max(t.sal) from @test t where t.deptid = w.deptid and t.sal < w.ms) Peter LarssonHelsingborg, Sweden |
 |
|
khurrat
Starting Member
6 Posts |
Posted - 2006-11-10 : 05:39:04
|
wow!...it works!!..thanks for this peter..I will definately work hard to write such queries n think in this way..can I ask a question??... if this is allowed hereHow long have been into this stream(sql server) ??..Thanks again!!.. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 05:43:16
|
I have worked with SQL Server since early 1994.Peter LarssonHelsingborg, Sweden |
 |
|
khurrat
Starting Member
6 Posts |
Posted - 2006-11-10 : 05:52:03
|
GOSH!!..Thats why my question irritated you ,rt?? nways , thanks for being an inspiration. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-10 : 06:13:32
|
No, I was irritated by you not doing your homework.Or, if it is a interview question, you are cheating yourself by getting that position with not enough skills.Peter LarssonHelsingborg, Sweden |
 |
|
khurrat
Starting Member
6 Posts |
Posted - 2006-11-10 : 06:34:30
|
I m not into this stream...I m tryin to learn sql server in my leisure time..N struggled through this one tryin to explore few concepts..using subqueries.... but should have tried a lil more before posting.. I ll do that next time... |
 |
|
|
|
|
|
|