Problem 18. *3rd Highest Salary - judge
Здравейте,
Решавам задача "Problem 18. *3rd Highest Salary" / Exercises: Data Aggregation, и изкарвам отговора с това querry, но judge не го приема за верен:
CREATE TABLE tempDB (DepartmentID INT, ThirdHighestSalary DECIMAL(10,2));
DECLARE @cnt INT = (SELECT DISTINCT min(departmentId) FROM Employees)
DECLARE @cnt_total int = (SELECT DISTINCT max(departmentId) FROM Employees)
WHILE @cnt <= @cnt_total
BEGIN
INSERT INTO tempDB (DepartmentID,ThirdHighestSalary)
SELECT DISTINCT departmentId, Salary AS [ThirdHighestSalary] FROM Employees
WHERE DepartmentID = @cnt ORDER BY Salary DESC OFFSET 2 ROW FETCH NEXT 1 ROW only
SET @cnt = @cnt + 1;
END;
SELECT * FROM tempDB;
Това querry изкарва същият отговор и judge го приема:
SELECT DepartmentID,
(SELECT DISTINCT Salary FROM Employees
WHERE DepartmentID = e.DepartmentID
ORDER BY Salary DESC OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY) AS ThirdHighestSalary
FROM Employees e
WHERE (SELECT DISTINCT Salary FROM Employees
WHERE DepartmentID = e.DepartmentID
ORDER BY Salary DESC OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY) IS NOT NULL
GROUP BY DepartmentID
Можете ли да ми кажете на какво се дължи това ?
малко подобрение за да избегнем повтарянето
Аз лично е реших така:
И аз реших така:
SELECT DISTINCT DepartmentID,Salary AS 'ThirdHighestSalary'
FROM(
SELECT Employees.DepartmentID,Employees.Salary,DENSE_RANK() OVER
(PARTITION BY Employees.DepartmentID ORDER BY Employees.Salary DESC) AS Rank
FROM Employees
) AS Tbl01
WHERE Tbl01.Rank=3
ИЛИ
SELECT DepartmentID,Salary AS [ThirdHighestSalary]
FROM(
SELECT Employees.DepartmentID,Employees.Salary,DENSE_RANK() OVER
(PARTITION BY Employees.DepartmentID ORDER BY Employees.Salary DESC) AS Rank
FROM Employees
) AS Tbl01
WHERE Tbl01.Rank=3
GROUP BY DepartmentID,Salary