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

时间:2022-12-23 22:56:59

I want to do something like this

我想做这样的事情

SELECT ID, X, Y, Z FROM TABLE_1

this returns me something like:

这会让我回复:

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

And i have another table TABLE_2 that has a column with the column names from the previous table (TABLE_1):

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

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

and now i need to insert all my data from the select into this new table TABLE_3 and it has to be like this:

现在我需要将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     |

Any suggestion on a simple way to do it?

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

3 个解决方案

#1


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

#2


2  

You can use the UNPIVOT table operator to do this, like so:

您可以使用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

This will give you:

这会给你:

| 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


2  

Depending on your version of SQL Server you can use the UNPIVOT function on the first table, apply a row_number() to each table and then join on the row number. The UNPIVOT function takes the columns X, Y, and Z and converts them to rows. Once this conversion is done you will give each record a row_number() that is partitioned by the IDTABLE_1.

根据您的SQL Server版本,您可以在第一个表上使用UNPIVOT函数,将row_number()应用于每个表,然后连接行号。 UNPIVOT函数采用X,Y和Z列并将它们转换为行。完成此转换后,您将为每条记录提供由IDTABLE_1分区的row_number()。

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;

See SQL Fiddle with Demo

请参阅SQL Fiddle with Demo

The result is:

结果是:

| 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 |

#1


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

#2


2  

You can use the UNPIVOT table operator to do this, like so:

您可以使用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

This will give you:

这会给你:

| 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


2  

Depending on your version of SQL Server you can use the UNPIVOT function on the first table, apply a row_number() to each table and then join on the row number. The UNPIVOT function takes the columns X, Y, and Z and converts them to rows. Once this conversion is done you will give each record a row_number() that is partitioned by the IDTABLE_1.

根据您的SQL Server版本,您可以在第一个表上使用UNPIVOT函数,将row_number()应用于每个表,然后连接行号。 UNPIVOT函数采用X,Y和Z列并将它们转换为行。完成此转换后,您将为每条记录提供由IDTABLE_1分区的row_number()。

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;

See SQL Fiddle with Demo

请参阅SQL Fiddle with Demo

The result is:

结果是:

| 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 |