SQL joining column from same table

  Kiến thức lập trình

I need to create a view from the table by joining the columns from the same table.
Here is how my table has been created

CREATE TABLE tbl (emp VARCHAR PRIMARY KEY, col1 VARCHAR, col2 VARCHAR);

INSERT INTO tbl VALUES ('one', 'A','B');
INSERT INTO tbl VALUES ('two', 'B',NULL);
INSERT INTO tbl VALUES ('three', 'D',NULL);
INSERT INTO tbl VALUES ('four', NULL,NULL);
INSERT INTO tbl VALUES ('five', NULL,NULL);

which looks like this, the empty value in the following table denotes the NULL

D SELECT * FROM tbl;
┌─────────┬─────────┬─────────┐
│   emp   │  col1   │  col2   │
│ varchar │ varchar │ varchar │
├─────────┼─────────┼─────────┤
│ one     │ A       │ B       │
│ two     │ B       │         │
│ three   │ D       │         │
│ four    │         │         │
│ five    │         │         │
└─────────┴─────────┴─────────┘

The view I need is the cross product of the first column with rest ones and should look like

D SELECT * FROM out;
┌─────────┬─────────┐
│   emp   │   col   │
│ varchar │ varchar │
├─────────┼─────────┤
│ one     │ A       │
│ one     │ B       │
│ two     │ B       │
│ three   │ D       │
└─────────┴─────────┘

LEAVE A COMMENT