| Author |
Topic |
|
archanasql
Starting Member
27 Posts |
Posted - 2008-03-13 : 02:25:43
|
| Hi,I have a table like this..empno empname dept sal----------- ------------------------------ -------------------- -1 mike HR 40002 John HR 20003 Patty HR 35004 Carly HR 22005 ABC HR 10006 DEF HR 45007 Ben HR 22008 HGI IT 40009 GOG IT 200010 LILY IT 350011 Cathy IT 220012 Paul IT 100013 Ben IT 450014 Ben Fin 220015 HGI Fin 400016 GOG Fin 200017 LILY Fin 350018 Cathy Fin 220019 Paul Fin 1080020 Ben Fin 4500How will i get Maximum and Minimum 3 salaries of EACH DEPARTMENTie i want to know who are the employees who are drawing top 3 max and min salaries in each department? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-13 : 03:31:51
|
Are you using SQL Server 2000 or SQL Server 2005? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
archanasql
Starting Member
27 Posts |
Posted - 2008-03-13 : 10:57:30
|
| sql server 2005 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-03-13 : 11:14:22
|
quote: Originally posted by X002548 SELECT * FROM (SELECT TOP 3 DEPT, SALFROM TABLEORDER BY SAL) AS AUNION ALLSELECT * FROM (SELECT TOP 3 DEPT, SALFROM TABLEORDER BY SAL DESC) AS B
Brett:quote: How will i get Maximum and Minimum 3 salaries of EACH DEPARTMENT
- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-13 : 11:42:13
|
using ROW_NUMBER function:-;With Sal_CTE (Low,High,EmpNo,EmpName,Dept,Sal) AS(SELECT ROW_NUMBER() OVER(PARTITION BY dept ORDER BY sal) AS Low, ROW_NUMBER() OVER(PARTITION BY dept ORDER BY sal DESC) AS High,empno,empname,dept,salFROM YourTable)SELECT t.dept,LEFT(ls.lowsal,LEN(ls.lowsal)-1) AS 'least sal employees',LEFT(hs.highsal,LEN(hs.highsal)-1) AS 'max sal employees'FROM (SELECT DISTINCT dept FROM YourTable)tCROSS APPLY (SELECT EmpName+',' AS [text()] FROM Sal_CTE WHERE Dept=t.dept AND High<=3 ORDER BY High FOR XML PATH(''))hs(highsal)CROSS APPLY (SELECT EmpName+',' AS [text()] FROM Sal_CTE WHERE Dept=t.dept AND Low<=3 ORDER BY Low FOR XML PATH(''))ls(lowsal)output---------------------dept least sal employees max sal employees----- -----------------------------------------Fin GOG,Ben,Cathy Paul,Ben,HGIHR ABC,John,Carly DEF,mike,PattyIT Paul,GOG,Cathy Ben,HGI,LILY |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-13 : 11:54:38
|
Yikes.Keep the CTE definition and simply writeselect * from sal_ctewhere low <= 3 or high <= 3 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-03-13 : 11:56:53
|
What's with all the cross apply and concatenation? We should be returning data from our database, not strings.select * from( select Dept, 'High' as Type, row_number() over (partition by dept order by sal DESC) as Rank, sal, EmpName from Yourtable union all select Dept, 'Low' as Type, row_number() over (partition by dept order by sal ASC) as Rank, sal, EmpName from Yourtable) xwhere x.rank <=3order by Dept, Type, Rank You can use a CTE instead as well. ..- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
archanasql
Starting Member
27 Posts |
Posted - 2008-03-13 : 12:12:43
|
| Thanks for your reply.Can we do it as a subquery?ie.Using distinct option or Grouping by dept ? |
 |
|
|
archanasql
Starting Member
27 Posts |
Posted - 2008-03-13 : 12:16:22
|
| Hi Visakh,thanks for your reply. I want the salaries too along with names of employees. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
archanasql
Starting Member
27 Posts |
Posted - 2008-03-13 : 12:33:42
|
| Sorry Jeff,I missed your reply..Thanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-13 : 12:54:29
|
quote: Originally posted by jsmith8858 What's with all the cross apply and concatenation? We should be returning data from our database, not strings.select * from( select Dept, 'High' as Type, row_number() over (partition by dept order by sal DESC) as Rank, sal, EmpName from Yourtable union all select Dept, 'Low' as Type, row_number() over (partition by dept order by sal ASC) as Rank, sal, EmpName from Yourtable) xwhere x.rank <=3order by Dept, Type, Rank You can use a CTE instead as well. ..- Jeffhttp://weblogs.sqlteam.com/JeffS
just gave another format of output. Can take it directly from db if you want to. |
 |
|
|
archanasql
Starting Member
27 Posts |
Posted - 2008-03-13 : 14:02:15
|
| what is the functionality of OVER inover (partition by dept,,,,,)in the above query? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-14 : 02:10:21
|
Read Books Online about Windowed Functions.ROW_NUMBER(), DENSE_RANK(), RANK(), NTILE() to mention a few. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|