(
[EmpCode] INT IDENTITY (1,1),
[EmpName] VARCHAR(100) NOT NULL,
[Salary] NUMERIC(10,2) NOT NULL,
[DeptName] VARCHAR(100) NOT NULL
)
GO
[EmpCode],[EmpName],[Salary]
FROM
[DBO].[Employee] WITH (NOLOCK)
GO
[EmpCode],[EmpName],[Salary]
FROM
[DBO].[Employee] WITH (NOLOCK)
ORDER BY
[Salary] DESC
GO
[EmpCode],[EmpName],[Salary]
FROM
[EmpCode],[EmpName],[Salary]
,RANK() OVER (ORDER BY [Salary] DESC) AS [Highest]
FROM
[DBO].[Employee] WITH (NOLOCK)
) AS High
WHERE
[Highest] = 4
GO
Yeap, this is what we expected right. J
[EmpCode],[EmpName],[Salary]
FROM
[DBO].[Employee] WITH (NOLOCK)
GO
[EmpCode],[EmpName],[Salary]
FROM
[EmpCode],[EmpName],[Salary]
,RANK() OVER (ORDER BY [Salary] DESC) AS [Highest]
FROM
[DBO].[Employee] WITH (NOLOCK)
) AS High
WHERE
[Highest] = 4
GO
[EmpCode],[EmpName],[Salary],[Highest]
FROM
(
SELECT
[EmpCode],[EmpName],[Salary]
,RANK() OVER (ORDER BY [Salary] DESC) AS [Highest]
FROM
[DBO].[Employee] WITH (NOLOCK)
) AS High
GO
[EmpCode],[EmpName],[Salary],[Highest]
FROM
(
SELECT
[EmpCode],[EmpName],[Salary]
, DENSE_RANK() OVER (ORDER BY [Salary] DESC) AS [Highest]
FROM
[DBO].[Employee] WITH (NOLOCK)
) AS High
GO
[EmpCode],[EmpName],[Salary]
FROM
[EmpCode],[EmpName],[Salary]
, DENSE_RANK() OVER (ORDER BY [Salary] DESC) AS [Highest]
FROM
[DBO].[Employee] WITH (NOLOCK)
) AS High
WHERE
[Highest] = 4
GO
[EmpCode],[EmpName],[Salary]
FROM
(
SELECT
[EmpCode],[EmpName],[Salary]
, DENSE_RANK() OVER (ORDER BY [Salary] DESC) AS [Highest]
FROM
[DBO].[Employee] WITH (NOLOCK)
) AS High
WHERE
[Highest] = @iHightest
GO
SET @iHightest = 2
SELECT
[EmpCode],[EmpName],[Salary],[DeptName]
FROM
(
SELECT
[EmpCode],[EmpName],[Salary],[DeptName]
, DENSE_RANK() OVER (PARTITION BY [DeptName] ORDER BY [Salary] DESC) AS [Highest]
FROM
[DBO].[Employee] WITH (NOLOCK)
) AS High
WHERE
[Highest] = @iHightest
ORDER BY
GO
- RANK()or DENSE_RANK()will work in SQL Server 2005 and above versions.
- For more info refer http://msdn.microsoft.com/en-us/library/ms173825