gpt4 book ai didi

sql-server - 获取第四行查询结果

转载 作者:行者123 更新时间:2023-12-05 00:00:27 24 4
gpt4 key购买 nike

我正在使用 Microsoft SQL Server 2012 并且我有这个查询(如下)。它正确返回所有内容,但我无法弄清楚如何只返回第 4 行。

我知道如何用一个简单的查询来做到这一点,但是对于一个如此复杂的查询,我不知道如何只返回第 4 行。由于我使用的是 MS SQL Server,因此无法使用限制/偏移。我应该如何以及在何处放置代码以仅打印第 4 行?我很难过。谢谢。

SELECT T.[First Name],
T.[Last Name],
T.[Middle Name],
T.[Country Name],
T.[Designation Name],
Salary.Emp_Salary AS 'Current Salary'
FROM
(
SELECT
Emp_First_Name AS "First Name",
Emp_Last_Name AS "Last Name",
Emp_Middle_Name AS "Middle Name",
Country_Name AS "Country Name",
Desig_Name as "Designation Name",
MAX(Emp_Salary_Change_Year) AS "Time"
FROM Employee_Details

INNER JOIN Country ON Employee_Details.Emp_Country_Id = Country.Country_Id
INNER JOIN State ON Employee_Details.Emp_State_Id = State.State_Id
INNER JOIN Designation ON Employee_Details.Desig_Id = Designation.Desig_Id
INNER JOIN Salary ON Employee_Details.Emp_Id = Salary.Emp_Id

GROUP BY Emp_First_Name, Emp_Last_Name,
Emp_Middle_Name, Country_Name, Desig_Name) AS T
INNER JOIN Salary ON T.Time = Salary.Emp_Salary_Change_Year
ORDER BY [Current Salary];

最佳答案

将 CTE 与 ROW_NUMBER() 结合使用

;WITH Cte AS
(
SELECT Col1, Col2, ...,
ROW_NUMBER() OVER (ORDER BY ...) AS RowNum
FROM Table
)
SELECT *
FROM Cte
WHERE RowNum = 4

关于sql-server - 获取第四行查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40412868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com