我有一个表,该表包含名为IgniteConfiguration.SpringConfigUrl
和A
的2列,其定义为:
B
预期输出为:
A B
c1 2
c2 3
c3 4
答案 0 :(得分:1)
CREATE TABLE #table2
([A] varchar(2), [B] int)
;
INSERT INTO #table2
([A], [B])
VALUES
('c1', 2),
('c2', 3),
('c3', 4)
;WITH nums AS
(SELECT 1 AS value ,a,b from #table2
UNION ALL
SELECT value + 1 AS value ,A,b
FROM nums
WHERE nums.value <B)
SELECT a,value
FROM nums order by a,value
输出
a value
c1 1
c1 2
c2 1
c2 2
c2 3
c3 1
c3 2
c3 3
c3 4
答案 1 :(得分:0)
创建了一个表值函数,在该函数中,我使用了递归cte来评估put中给定的重复值,然后使用交叉应用将该表与函数连接起来
CREATE FUNCTION [dbo].[udf_GetData] (
@Data INT
)
RETURNS @output TABLE (
Data INT
)
BEGIN
;WITH CTe
AS
(
SELECT 1 As Data
UNION ALL
SELECT Data+1
FROM CTe
WHERE Data < @Data
)
INSERT INTO @output
SELECT Data FROM CTe
RETURN
END
采样数据并说明了如何使用CROSS APPLY调用函数
DECLARE @Data AS TABLE (A VARCHAR(10),B INT)
INSERT INTO @Data
SELECT 'c1', 2 UNION ALL
SELECT 'c2', 3 UNION ALL
SELECT 'c3', 4
SELECT d.A,
(SELECT [dbo].[udf_GetData](d.B)) AS RecData
FROM @Data d
结果
A RecursiveData
----------------
c1 1
c1 2
c2 1
c2 2
c2 3
c3 1
c3 2
c3 3
c3 4
答案 2 :(得分:0)
您可以尝试以下方法:
// test data
declare @tbl table(A char(2), B int);
insert into @tbl values
('c1', 2),
('c2', 3),
('c3', 4);
// create CTE with numbers which we will need to join
declare @max int;
select @max = max(B) from @tbl;
;with numbers as (
select 1 n
union all
select n + 1 from numbers
where n < @max
)
// join numbers with your table
select A, n from @tbl t
join numbers n on t.B >= n.n
order by A, n
答案 3 :(得分:0)
说,您的表名是test。
WITH r(a, b, repeat) as
(SELECT a, b, 1 from test
union all
select a, b, repeat+1 from r
where r.repeat < r.b)
select * from r
ORDER BY a, repeat;