我要生成这样的结果假设我有这些记录
I am going to generate a result like this suppose I have this records
Salah 3
John 2
我会期待这个
Salah
Salah
Salah
John
John
一种方法,如果您总是对 n 使用 small 值,则使用 rCTE:
One method, if you always have a small value for n is to use a rCTE:
WITH rCTE AS(
SELECT [Name],
N,
1 AS I
FROM dbo.YourTable
UNION ALL
SELECT [Name],
N,
I + 1
FROM rCTE
WHERE I < N)
SELECT [Name]
FROM rCTe
ORDER BY [Name] DESC;
如果您有更大的数字,请使用性能更好的 Tally.我在这里使用内联:
If you have much larger numbers, use a more performamt Tally. I use an inline here:
WITH N AS(
SELECT N
FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
SELECT --TOP (SELECT MAX(N) FROM dbo.YourTable) --Limits the number of rows, which could also provide a performance benefit if you only sometimes have large numbers
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
FROM N N1, N N2, N N3) --1000 rows, add more cross joins for more rows
SELECT [Name]
FROM dbo.YourTable YT
JOIN Tally T ON YT.N >= T.I
ORDER BY YT.[Name] DESC;
db<>fiddle
这篇关于生成从 1 到特定值的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
修改现有小数位信息Modify Existing decimal places info(修改现有小数位信息)
多次指定相关名称“CONVERT"The correlation name #39;CONVERT#39; is specified multiple times(多次指定相关名称“CONVERT)
T-SQL 左连接不返回空列T-SQL left join not returning null columns(T-SQL 左连接不返回空列)
从逗号或管道运算符字符串中删除重复项remove duplicates from comma or pipeline operator string(从逗号或管道运算符字符串中删除重复项)
将迭代查询更改为基于关系集的查询Change an iterative query to a relational set-based query(将迭代查询更改为基于关系集的查询)
将零连接到 sql server 选择值仍然显示 4 位而不是concatenate a zero onto sql server select value shows 4 digits still and not 5(将零连接到 sql server 选择值仍然显示 4 位而不是 5)