Extract one column from sql table and insert into another table as multiple records coupled with different values

gnaanaa

I need to extract one column from sql table and copy it to another table coupled with multiple values. For example, on the source table I select Col1, and copy over to resulting table with first column auto incremental, second column is the column value copied over from source, and third column will be another numeric value, but we need to create 3 records with values 10,20, and 30 for each copied column value.

Source table:

+--------+---------+------+
|  Col1  |  Col2   | Col3 |
+--------+---------+------+
| Value1 | Value10 |  123 |
| Value2 | Value20 |  123 |
| Value3 | Value30 |  123 |
+--------+---------+------+

secondary table:

+------+--------+-------+
| ColI |  Col1  | ColII |
+------+--------+-------+
|    1 | Value1 |    10 |
|    2 | Value1 |    20 |
|    3 | Value1 |    30 |
|    4 | Value2 |    10 |
|    5 | Value2 |    20 |
|    6 | Value2 |    30 |
+------+--------+-------+

What query (MS SQL) to apply on this scenario? The second table is already created with Auto incremental first column, and the multiple values are always (10,20, and 30).

Justin

Query: SQLFIddleExample

SELECT t1.Col1, t2.COLII
FROM Table1 t1
CROSS JOIN (SELECT 10 as COLII
            UNION ALL
            SELECT 20 as COLII
            UNION ALL
            SELECT 30 as COLII) t2

Result:

|   Col1 | COLII |
|--------|-------|
| Value1 |    10 |
| Value1 |    20 |
| Value1 |    30 |
| Value2 |    10 |
| Value2 |    20 |
| Value2 |    30 |
| Value3 |    30 |
| Value3 |    10 |
| Value3 |    20 |

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql insert unique values from one column to a column of another table

From Dev

insert statement one column from another table rest of the columns is values

From Dev

Insert INTO from one table to another and change column values

From Dev

select multiple column from one table and insert into another as rows

From Dev

Select / Update records in one table that are LIKE words selected from another table column with multiple records

From Dev

Join multiple values from one column, selected from another table

From Dev

Insert in SQL table using select with column values from another select

From Dev

How to insert from one table into another with extra values in SQL?

From Dev

How to insert multiple records from one table into another on update of third table in MySQL

From Dev

Insert from one table to another (different databases)

From Dev

insert records into table where one filed is from another table

From Dev

Insert records into one table using key from another table

From Dev

SQL - Selecting a column from another table twice with different values

From Dev

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

From Dev

Insert several values into one column of a SQL table

From Dev

Retrieving records from one table based in 'similar' values of another table

From Dev

How to get values from one table column, concatenate them with a string and insert them in another table?

From Dev

How to get values from one table column, concatenate them with a string and insert them in another table?

From Dev

Display records from one table which is not in another table by multiple columns

From Dev

Inserting values of one column of a table into different columns of another table

From Dev

SQL Server: Insert Multiple Rows to a table based on a column in a different table

From Dev

insert data from one table to a single column another table with no relation

From Dev

Insert distinct values from one table into another table

From Dev

How to insert values into one table from another table

From Dev

Insert Data From One Table to Another Table with default values

From Dev

Insert Data From One Table to Another Table and Add New Values

From Dev

How to insert records from one table to another using trigger in SQL Server?

From Dev

Insert records from one table to another without violating any constraint in SQL Server database

From Dev

Insert records from one table to another with and user data..SQL server

Related Related

  1. 1

    mysql insert unique values from one column to a column of another table

  2. 2

    insert statement one column from another table rest of the columns is values

  3. 3

    Insert INTO from one table to another and change column values

  4. 4

    select multiple column from one table and insert into another as rows

  5. 5

    Select / Update records in one table that are LIKE words selected from another table column with multiple records

  6. 6

    Join multiple values from one column, selected from another table

  7. 7

    Insert in SQL table using select with column values from another select

  8. 8

    How to insert from one table into another with extra values in SQL?

  9. 9

    How to insert multiple records from one table into another on update of third table in MySQL

  10. 10

    Insert from one table to another (different databases)

  11. 11

    insert records into table where one filed is from another table

  12. 12

    Insert records into one table using key from another table

  13. 13

    SQL - Selecting a column from another table twice with different values

  14. 14

    How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

  15. 15

    Insert several values into one column of a SQL table

  16. 16

    Retrieving records from one table based in 'similar' values of another table

  17. 17

    How to get values from one table column, concatenate them with a string and insert them in another table?

  18. 18

    How to get values from one table column, concatenate them with a string and insert them in another table?

  19. 19

    Display records from one table which is not in another table by multiple columns

  20. 20

    Inserting values of one column of a table into different columns of another table

  21. 21

    SQL Server: Insert Multiple Rows to a table based on a column in a different table

  22. 22

    insert data from one table to a single column another table with no relation

  23. 23

    Insert distinct values from one table into another table

  24. 24

    How to insert values into one table from another table

  25. 25

    Insert Data From One Table to Another Table with default values

  26. 26

    Insert Data From One Table to Another Table and Add New Values

  27. 27

    How to insert records from one table to another using trigger in SQL Server?

  28. 28

    Insert records from one table to another without violating any constraint in SQL Server database

  29. 29

    Insert records from one table to another with and user data..SQL server

HotTag

Archive