对于选择的每一行,将多行插入一个表中

时间:2013-01-09 17:52:35

标签: sql sql-server select insert

我想做这样的事情

SELECT ID, X, Y, Z FROM TABLE_1

这会让我回复:

| IDTABLE_1 |  X  |  Y  |  Z  |
| 1         |  a  |  b  |  c  |
| 2         |  d  |  e  |  f  |
| 3         |  g  |  h  |  i  |

我有另一个表TABLE_2,其中有一列包含上一表(TABLE_1)中的列名:

| IDTABLE_2 | COLUMN | 
| 101       | X      | 
| 102       | Y      |
| 103       | Z      |

现在我需要将select中的所有数据插入到这个新表TABLE_3中,它必须是这样的:

| IDTABLE_3 | IDTABLE_1 | IDTABLE_2   | VALUE |
| 201       | 1         | 101         | a     |
| 202       | 1         | 102         | b     |
| 203       | 1         | 103         | c     |
| 204       | 2         | 101         | d     |
| 205       | 2         | 102         | e     |
| 206       | 2         | 103         | f     |
| 207       | 3         | 101         | g     |
| 208       | 3         | 102         | h     |
| 209       | 3         | 103         | i     |

有关简单方法的任何建议吗?

3 个答案:

答案 0 :(得分:5)

SELECT
   IDENTITY(int, 200, 1) IDTABLE_3,
   T1.IDTABLE_1,
   T2.IDTABLE_2,
   CASE T2.COLUMN WHEN 'X' THEN T1.X
                  WHEN 'Y' THEN T1.Y
                  WHEN 'Z' THEN T1.Z END VALUE
INTO TABLE_3
FROM TABLE_1 T1
  CROSS JOIN TABLE_2 T2

答案 1 :(得分:2)

您可以使用UNPIVOT表运算符执行此操作,如下所示:

WITH CTE
AS
(
  SELECT IDTABLE_1, ID, value
  FROM Table_1
  UNPIVOT
  (
    ID
    FOR value   IN(x, y,z)
  ) u
)
SELECT 
  ROW_NUMBER() OVER(ORDER BY t1.ID) + 200 AS IDTABLE_3 ,
  t1.IDTABLE_1,
  t2.[IDTABLE_2],
  t1.ID AS VALUE 
FROM CTE t1
INNER JOIN TABLE_2 t2 ON t1.value = t2."Column";

SQL Fiddle Demo

这会给你:

| IDTABLE_3 | IDTABLE_1 | IDTABLE_2 | VALUE |
---------------------------------------------
|       201 |         1 |       101 |     a |
|       202 |         1 |       102 |     b |
|       203 |         1 |       103 |     c |
|       204 |         2 |       101 |     d |
|       205 |         2 |       102 |     e |
|       206 |         2 |       103 |     f |
|       207 |         3 |       101 |     g |
|       208 |         3 |       102 |     h |
|       209 |         3 |       103 |     i |

答案 2 :(得分:2)

根据您的SQL Server版本,您可以使用第一个表上的UNPIVOT函数,对每个表应用row_number(),然后加入行号。 UNPIVOT函数使用列XYZ并将其转换为行。完成此转换后,您将为每条记录提供由row_number()分区的IDTABLE_1

select row_number() over(order by t1.IDTABLE_1) + 200 AS IDTABLE_3,
  t1.IDTABLE_1, t2.IDTABLE_2, t1.value
from
(
  select IDTABLE_1, value, col,
      row_number() over(partition by IDTABLE_1 order by col) rn
  from table1
  unpivot
  (
    value
    for col in (X, Y, Z)
  ) unpiv
) t1
inner join
(
  select IDTABLE_2, [COLUMN],
    row_number() over(order by IDTABLE_2) rn
  from Table2
) t2
  on t1.rn = t2.rn
order by t1.IDTABLE_1;

请参阅SQL Fiddle with Demo

结果是:

| IDTABLE_3 | IDTABLE_1 | IDTABLE_2 | VALUE |
---------------------------------------------
|       201 |         1 |       101 |     a |
|       202 |         1 |       102 |     b |
|       203 |         1 |       103 |     c |
|       204 |         2 |       101 |     d |
|       205 |         2 |       102 |     e |
|       206 |         2 |       103 |     f |
|       207 |         3 |       101 |     g |
|       208 |         3 |       102 |     h |
|       209 |         3 |       103 |     i |