如何动态跳过顶行 SQL

How to dynamically skip top rows SQL

我一直在尝试找出如何动态地跳过行并从这个 table 中用 departmentIDdepartmentName 创建一个新的 table unnamed_1unnamed_2。我试图让程序自动获取其中包含 departmentNamedepartmentID 的行。我附上图片以供参考。

编辑: 主要问题是我试图找到一种方法自动将其替换为 unnamed_1_2这些 departmentID 和名称而无需修改 运行 程序之外的选项。

看看这个,可能会有帮助:

SELECT departmentId, departmentName FROM
(SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNumber, 
    C.RowNumber AS RowLimit, 
    B.Unamed_1 AS departmentId, 
    B.Unamed_2 AS departmentName 
FROM [WhatEverTableItIs] AS B
INNER JOIN (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNumber,
        Unamed_1, 
        Unamed_2 
    FROM [WhatEverTableItIs]) AS C 
ON C.Unamed_1 = 'departmentID' AND C.Unamed_2 = 'departmentName') A
WHERE RowNumber > RowLimit

它将在 departmentIDUnamed_1departmentNameUnamed_2 的行上将 table 与自身连接起来。它计算行号并将其用作键(就像主键一样),然后过滤行号小于我们加入的特定行号的行 tables on.