SQL Server:为每行添加行?

时间:2017-08-31 23:23:22

标签: sql-server tsql

我在SQL Server中有一个像这样的表:

   Col1 Col2  Col3
   ----- ----  -----
   1      1      1
   0.5   0.5     2
   0.3   0.1     3

我想要做的是,对于Col 3中的每个值,1,2,3,按顺序添加包含数字1-53的第4列。所以,像:

    Col1 Col2  Col3     Col 4
   ----- ----  -----   ------
   1      1      1       1
   1      1      1       2
   1      1      1       3

等等。

我怎样才能在T-SQL / Microsoft SQL Server 2016中实现这一目标?

谢谢!

3 个答案:

答案 0 :(得分:2)

这些是你想要获得的结果吗?

IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL 
DROP TABLE #TestData;

CREATE TABLE #TestData (
    Col1 DECIMAL(9,1) NOT NULL,
    Col2 DECIMAL(9,1) NOT NULL,
    Col3 INT NOT NULL 
    );
INSERT #TestData (Col1, Col2, Col3) VALUES
    (1, 1 ,1), (0.5,0.5,2), (0.3,0.1,3);

SELECT 
    td.Col1, td.Col2, td.Col3, Col4 = t.n
FROM
    #TestData td
    CROSS APPLY dbo.tfn_Tally(53, 1) t;

结果...

Col1  Col2  Col3 Col4
----- ----- ---- -----
1.0   1.0   1    1
0.5   0.5   2    1
0.3   0.1   3    1
1.0   1.0   1    2
0.5   0.5   2    2
0.3   0.1   3    2
1.0   1.0   1    3
0.5   0.5   2    3
0.3   0.1   3    3
1.0   1.0   1    4
0.5   0.5   2    4
0.3   0.1   3    4
1.0   1.0   1    5
0.5   0.5   2    5
0.3   0.1   3    5
1.0   1.0   1    6
0.5   0.5   2    6
0.3   0.1   3    6
1.0   1.0   1    7
0.5   0.5   2    7
0.3   0.1   3    7
1.0   1.0   1    8
0.5   0.5   2    8
0.3   0.1   3    8
1.0   1.0   1    9
0.5   0.5   2    9
0.3   0.1   3    9
1.0   1.0   1    10
0.5   0.5   2    10
0.3   0.1   3    10
1.0   1.0   1    11
0.5   0.5   2    11
0.3   0.1   3    11
1.0   1.0   1    12
0.5   0.5   2    12
0.3   0.1   3    12
1.0   1.0   1    13
0.5   0.5   2    13
0.3   0.1   3    13
1.0   1.0   1    14
0.5   0.5   2    14
0.3   0.1   3    14
1.0   1.0   1    15
0.5   0.5   2    15
0.3   0.1   3    15
1.0   1.0   1    16
0.5   0.5   2    16
0.3   0.1   3    16
1.0   1.0   1    17
0.5   0.5   2    17
0.3   0.1   3    17
1.0   1.0   1    18
0.5   0.5   2    18
0.3   0.1   3    18
1.0   1.0   1    19
0.5   0.5   2    19
0.3   0.1   3    19
1.0   1.0   1    20
0.5   0.5   2    20
0.3   0.1   3    20
1.0   1.0   1    21
0.5   0.5   2    21
0.3   0.1   3    21
1.0   1.0   1    22
0.5   0.5   2    22
0.3   0.1   3    22
1.0   1.0   1    23
0.5   0.5   2    23
0.3   0.1   3    23
1.0   1.0   1    24
0.5   0.5   2    24
0.3   0.1   3    24
1.0   1.0   1    25
0.5   0.5   2    25
0.3   0.1   3    25
1.0   1.0   1    26
0.5   0.5   2    26
0.3   0.1   3    26
1.0   1.0   1    27
0.5   0.5   2    27
0.3   0.1   3    27
1.0   1.0   1    28
0.5   0.5   2    28
0.3   0.1   3    28
1.0   1.0   1    29
0.5   0.5   2    29
0.3   0.1   3    29
1.0   1.0   1    30
0.5   0.5   2    30
0.3   0.1   3    30
1.0   1.0   1    31
0.5   0.5   2    31
0.3   0.1   3    31
1.0   1.0   1    32
0.5   0.5   2    32
0.3   0.1   3    32
1.0   1.0   1    33
0.5   0.5   2    33
0.3   0.1   3    33
1.0   1.0   1    34
0.5   0.5   2    34
0.3   0.1   3    34
1.0   1.0   1    35
0.5   0.5   2    35
0.3   0.1   3    35
1.0   1.0   1    36
0.5   0.5   2    36
0.3   0.1   3    36
1.0   1.0   1    37
0.5   0.5   2    37
0.3   0.1   3    37
1.0   1.0   1    38
0.5   0.5   2    38
0.3   0.1   3    38
1.0   1.0   1    39
0.5   0.5   2    39
0.3   0.1   3    39
1.0   1.0   1    40
0.5   0.5   2    40
0.3   0.1   3    40
1.0   1.0   1    41
0.5   0.5   2    41
0.3   0.1   3    41
1.0   1.0   1    42
0.5   0.5   2    42
0.3   0.1   3    42
1.0   1.0   1    43
0.5   0.5   2    43
0.3   0.1   3    43
1.0   1.0   1    44
0.5   0.5   2    44
0.3   0.1   3    44
1.0   1.0   1    45
0.5   0.5   2    45
0.3   0.1   3    45
1.0   1.0   1    46
0.5   0.5   2    46
0.3   0.1   3    46
1.0   1.0   1    47
0.5   0.5   2    47
0.3   0.1   3    47
1.0   1.0   1    48
0.5   0.5   2    48
0.3   0.1   3    48
1.0   1.0   1    49
0.5   0.5   2    49
0.3   0.1   3    49
1.0   1.0   1    50
0.5   0.5   2    50
0.3   0.1   3    50
1.0   1.0   1    51
0.5   0.5   2    51
0.3   0.1   3    51
1.0   1.0   1    52
0.5   0.5   2    52
0.3   0.1   3    52
1.0   1.0   1    53
0.5   0.5   2    53
0.3   0.1   3    53

答案 1 :(得分:1)

你必须发明一个带有数字的假表:

WITH nums as(
  SELECT 1 as num
  UNION ALL
  SELECT num + 1 FROM nums
  WHERE num <= 53
)

SELECT yourtable.*, num as col4 FROM
  Yourtable
  CROSS JOIN
  nums

答案 2 :(得分:0)

您可以使用以下代码。生成序列有很多种方法(可以将它存储在临时表中或使用cte)

CREATE TABLE temp
(

Col1 DECIMAL(10,1),
Col2 DECIMAL(10,1),
Col3 INT
)

INSERT INTO temp
VALUES 
(1,1,1)
,(0.5,0.5,2)
,(0.3,0.1,3)



DECLARE @Start INT =1
, @ENd INT = 53

SELECT 
t.* 
, seq.n AS Col4
FROM temp t
CROSS APPLY 
(
SELECT DISTINCT n = number 
FROM master..[spt_values] 
WHERE number BETWEEN @start AND @end

) seq

<强>结果:

Col1                                    Col2                                    Col3        Col4
--------------------------------------- --------------------------------------- ----------- -----------
1.0                                     1.0                                     1           1
1.0                                     1.0                                     1           2
1.0                                     1.0                                     1           3
1.0                                     1.0                                     1           4
1.0                                     1.0                                     1           5
1.0                                     1.0                                     1           6
1.0                                     1.0                                     1           7
1.0                                     1.0                                     1           8
1.0                                     1.0                                     1           9
1.0                                     1.0                                     1           10
1.0                                     1.0                                     1           11
1.0                                     1.0                                     1           12
1.0                                     1.0                                     1           13
1.0                                     1.0                                     1           14
1.0                                     1.0                                     1           15
1.0                                     1.0                                     1           16
1.0                                     1.0                                     1           17
1.0                                     1.0                                     1           18
1.0                                     1.0                                     1           19
1.0                                     1.0                                     1           20
1.0                                     1.0                                     1           21
1.0                                     1.0                                     1           22
1.0                                     1.0                                     1           23
1.0                                     1.0                                     1           24
1.0                                     1.0                                     1           25
1.0                                     1.0                                     1           26
1.0                                     1.0                                     1           27
1.0                                     1.0                                     1           28
1.0                                     1.0                                     1           29
1.0                                     1.0                                     1           30
1.0                                     1.0                                     1           31
1.0                                     1.0                                     1           32
1.0                                     1.0                                     1           33
1.0                                     1.0                                     1           34
1.0                                     1.0                                     1           35
1.0                                     1.0                                     1           36
1.0                                     1.0                                     1           37
1.0                                     1.0                                     1           38
1.0                                     1.0                                     1           39
1.0                                     1.0                                     1           40
1.0                                     1.0                                     1           41
1.0                                     1.0                                     1           42
1.0                                     1.0                                     1           43
1.0                                     1.0                                     1           44
1.0                                     1.0                                     1           45
1.0                                     1.0                                     1           46
1.0                                     1.0                                     1           47
1.0                                     1.0                                     1           48
1.0                                     1.0                                     1           49
1.0                                     1.0                                     1           50
1.0                                     1.0                                     1           51
1.0                                     1.0                                     1           52
1.0                                     1.0                                     1           53
0.5                                     0.5                                     2           1
0.5                                     0.5                                     2           2
0.5                                     0.5                                     2           3
0.5                                     0.5                                     2           4
0.5                                     0.5                                     2           5
0.5                                     0.5                                     2           6

依旧......