Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gagani
Posting Yak Master

112 Posts

Posted - 09/24/2012 :  04:19:55  Show Profile  Reply with Quote
1) I need a query which result in the output of second maximum salary in an employee table.
2) in which year the maximum number of employees hired

employeeid employeename salary hiredate
1 A 1000 01/02/2000
2 B 2000 02/02/2004
3 C 3000 05/09/2008
...........

bandi
Flowing Fount of Yak Knowledge

India
2242 Posts

Posted - 09/24/2012 :  05:41:17  Show Profile  Reply with Quote
1) SELECT MAX(salary) '2nd max sal'
FROM YourTable
WHERE salary < (SELECT MAX(salary) FROM YourTable)


2) SELECT TOP 1 datepart(yy, hire_date) Year, count(employee_id) cnt
FROM YourTable
GROUP BY datepart(yy, hire_date)
ORDER BY cnt DESC

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 09/24/2012 :  13:17:11  Show Profile  Reply with Quote

Alternate approach for 1
SELECT employeeid,employeename,salary,hiredate
(
SELECT DENSE_RANK() OVER (ORDER BY Salary DESC) AS Rnk,*
FROM table
)t
WHERE Rnk=2


modified solution for 2 to take care of cases where there are ties ie multiple years with same maximum employee count

SELECT TOP 1  WITH TIES datepart(yy, hire_date) Year, count(employee_id) cnt
FROM YourTable
GROUP BY datepart(yy, hire_date) 
ORDER BY cnt DESC


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000