Union table 自身限制最大行数

Union table to itself limiting to a maximum number of rows

我有这个table

ID         OtherId                info1                  info2                  startDate
---------- ---------------------- ---------------------- ---------------------- -----------------------
7160       1                      0                      0                      2001-01-01 00:00:00.000
7160       2                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       3                      0                      0                      2001-01-01 00:00:00.000
7160       4                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       5                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       6                      4.36                   0                      2001-01-01 00:00:00.000
7160       7                      0                      0                      2001-01-01 00:00:00.000

我想将相同的 table 合并到自身,但限制为最大行数。 例如限制为 16 行:

7160       1                      0                      0                      2001-01-01 00:00:00.000
7160       2                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       3                      0                      0                      2001-01-01 00:00:00.000
7160       4                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       5                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       6                      4.36                   0                      2001-01-01 00:00:00.000
7160       7                      0                      0                      2001-01-01 00:00:00.000
7160       1                      0                      0                      2001-01-01 00:00:00.000
7160       2                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       3                      0                      0                      2001-01-01 00:00:00.000
7160       4                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       5                      3.83                   4.42                   2001-01-01 00:00:00.000
7160       6                      4.36                   0                      2001-01-01 00:00:00.000
7160       7                      0                      0                      2001-01-01 00:00:00.000
7160       1                      0                      0                      2001-01-01 00:00:00.000
7160       2                      3.83                   4.42                   2001-01-01 00:00:00.000

我试过交叉连接,但这并不是我想要的。 你能给我一个提示吗?

谢谢!

您可以 select 从您的 table 使用交叉连接到计数 table(或 cte 形式的“内联”计数 table) :

WITH N0 AS (
    SELECT n
    FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9))V(n)
), Tally AS
(
    SELECT ROW_NUMBER() OVER(ORDER BY @@SPID) As N
    FROM N0 As ten, N0 As hundred --, N0 As thousand -- and so on
)

SELECT TOP 16 ID, OtherId, info1, info2, startDate
FROM TableName
CROSS JOIN Tally
ORDER BY N, OtherId

这会给你预期的结果。