单元格中的多个行数

时间:2016-01-14 11:17:09

标签: sql-server tsql

我有多个

CREATE TABLE multiple
(
    id int,
    Param1 int,
    Param2 int,
    Param3 int,
    Param4 int
);
INSERT INTO multiple VALUES 
    (1, 1, 2, 3, 0),
    (2, 3, 1, 0, 0),
    (3, 1, 2, 2, 1);
SELECT * FROM multiple;

我想用t-SQL这样的新表来实现:(行数= Param属性中的值)

Counter  ID TYPE
1        1  Param1
2        1  Param2
3        1  Param2
4        1  Param3
5        1  Param3
6        1  Param3
7        2  Param1
8        2  Param1
9        2  Param1
10       2  Param2
11       3  Param1
12       3  Param2
13       3  Param2
14       3  Param3
15       3  Param3
16       3  Param4

我在这里添加了这段文字,因为我在编辑器中有错误,看起来我的帖子主要是代码;

2 个答案:

答案 0 :(得分:2)

嘿,您可以使用以下代码来实现目标:

CREATE TABLE multiple
(
    id int,
    Param1 int,
    Param2 int,
    Param3 int,
    Param4 int
);
INSERT INTO multiple VALUES 
    (1, 1, 2, 3, 0),
    (2, 3, 1, 0, 0),
    (3, 1, 2, 2, 1);

SELECT * FROM multiple;

CREATE TABLE multiple1
(
    ID int,
    TYPE VARCHAR(10)
)
DECLARE @id int = 0

WHILE (@id<=(SELECT Count(id) FROM multiple))
BEGIN
DECLARE @Count int = 0
DECLARE @Count1 int = 0
DECLARE @Count2 int = 0
DECLARE @Count3 int = 0

    WHILE(@Count<(SELECT Param1 FROM  multiple where id = @id)) 
    BEGIN
        INSERT INTO multiple1 VALUES ((Select id from multiple where id = @id),'Param1')
        SET @Count = @Count + 1
    END
    WHILE(@Count1<(SELECT Param2 FROM  multiple where id = @id)) 
    BEGIN
        INSERT INTO multiple1 VALUES ((Select id from multiple where id = @id),'Param2')
        SET @Count1 = @Count1 + 1
    END
    WHILE(@Count2<(SELECT Param3 FROM  multiple where id = @id)) 
    BEGIN
        INSERT INTO multiple1 VALUES ((Select id from multiple where id = @id),'Param3')
        SET @Count2 = @Count2 + 1
    END
    WHILE(@Count3<(SELECT Param4 FROM  multiple where id = @id)) 
    BEGIN
        INSERT INTO multiple1 VALUES ((Select id from multiple where id = @id),'Param4')
        SET @Count3 = @Count3 + 1
    END

    SET @id = @id + 1
END

SELECT * FROM multiple1

答案 1 :(得分:1)

您听说过"tally tables"吗?他们会快速完成这项工作。这是一个使用一个的示例解决方案。它的工作原理是连接每个列,其中数字是&lt; = Param属性中的值,为您提供所需的行数。然后选择id和类型。

请注意,此示例使用的计数表最多只能计算256,所以如果您需要更多,可以添加额外的级别 - 只需确保将您的计数表放入临时表中,否则它将来自真正的快速执行,是一个非常缓慢的时间。

CREATE TABLE multiple
(
    id int,
    Param1 int,
    Param2 int,
    Param3 int,
    Param4 int
);
INSERT INTO multiple VALUES 
    (1, 1, 2, 3, 0),
    (2, 3, 1, 0, 0),
    (3, 1, 2, 2, 1);
SELECT * FROM multiple;


-- Tally table known as a "Ben-Gan" style Tally
WITH lv0 AS (SELECT 0 g UNION ALL SELECT 0)
    ,lv1 AS (SELECT 0 g FROM lv0 a CROSS JOIN lv0 b) -- 4
    ,lv2 AS (SELECT 0 g FROM lv1 a CROSS JOIN lv1 b) -- 16
    ,lv3 AS (SELECT 0 g FROM lv2 a CROSS JOIN lv2 b) -- 256
    ,Tally (n) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM lv3)
SELECT
    ROW_NUMBER() OVER (ORDER BY Id) [Counter],
    Id,
    [Type]
FROM
(
    SELECT Id, 'Param1' AS [Type] FROM multiple INNER JOIN Tally ON multiple.Param1 >= Tally.n UNION ALL
    SELECT Id, 'Param2' AS [Type] FROM multiple INNER JOIN Tally ON multiple.Param2 >= Tally.n UNION ALL
    SELECT Id, 'Param3' AS [Type] FROM multiple INNER JOIN Tally ON multiple.Param3 >= Tally.n UNION ALL
    SELECT Id, 'Param4' AS [Type] FROM multiple INNER JOIN Tally ON multiple.Param4 >= Tally.n
) [single]