skip to Main Content

Say I have a table, table1.

And table1 has 2 columns, "Vegetables" and "Fruits"

Using the values from table 1, how can I get the distinct of these 2 columns and use them to create a new column in a new table?

Attached is a sample table of input values, and expected output

I am using snowflake.

enter image description here

2

Answers


  1. Something like this?

    CREATE TABLE table2 (
    id INT PRIMARY KEY AUTO_INCREMENT,
      name VARCHAR(255)
    );
    
    INSERT INTO table2 (name)
    SELECT name FROM (
      SELECT vegetables AS name FROM table1
      UNION
      SELECT fruits AS name FROM table1
    ) AS names
    GROUP BY name;
    

    Check this out –> sqlfiddle

    Login or Signup to reply.
  2. SELECT DISTINCT column_name FROM
    (
      SELECT fruits AS column_name FROM table1
      UNION
      SELECT vegetables AS column_name FROM table2
    ) AS distinct_values;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search