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 |
|
liangtp@hotmail.com
Starting Member
27 Posts |
Posted - 2008-09-22 : 08:04:10
|
| Hi guys, I have a table below:UserID, Department, Salary1, HR, 1002, HR, 2553, HR, 3004, IT, 3555, IT, 4006, IT, 599Question: How shall i produce a query to select the lowest salary grouped by department? Something like below:UserID, Department, Salary1, HR, 1004, IT, 355Thanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 08:08:16
|
| [code]SELECT UserID, Department, SalaryFROM(SELECT ROW_NUMBER() OVER(PARTITION BY Department ORDER BY Salary) AS Seq,*FROM YourTable)tWHERE t.Seq=1[/code]or[code]SELECT t.*FROM YourTable tINNER JOIN (SELECT Department,MIN(Salary) AS MinSal FROM yourTable GROUP BY Department) tmpON tmp.Department=t.DepartmentAND tmp.MinSal=t.Salary[/code] |
 |
|
|
liangtp@hotmail.com
Starting Member
27 Posts |
Posted - 2008-09-22 : 20:47:26
|
| Thanks visakh16. Both methods worked fine for me. |
 |
|
|
|
|
|
|
|