【问题标题】:repeating / duplicating query entries based on a table value基于表值重复/复制查询条目
【发布时间】:2016-05-10 00:27:53
【问题描述】:
与此PostgreSQL主题相关/复制自:so-link
假设我有一个有两行的表
id | value |
----+-------+
1 | 2 |
2 | 3 |
我想编写一个查询,该查询将根据以下内容复制(重复)每一行
价值。我想要这个结果(总共 5 行):
id | value |
----+-------+
1 | 2 |
1 | 2 |
2 | 3 |
2 | 3 |
2 | 3 |
这在 SQL Anywhere (Sybase SQL) 中是如何实现的?
【问题讨论】:
标签:
sql
sybase
repeat
sqlanywhere
【解决方案1】:
最简单的方法是使用数字表。 . .一种生成整数。也许你有一个方便的。还有其他方法。例如,使用递归 CTE:
with numbers as (
select 1 as n
union all
select n + 1
from numbers
where n < 100
)
select t.*
from yourtable t join
numbers n
on n.n <= value;
并非所有版本的 Sybase 都必须支持递归 CTE。还有其他方法可以生成这样的表,或者您可能已经有了一种方便的方法。