Aan
Aan

Reputation: 12890

How to write this select statement?

If I have table1 contains Name, col2, col3 and table2 contains Name, NickName and col6.

I want to do select statement such I have a table like:

Name, NickName, col2, col3

where if table1 has 3 rows the new table should have the same.

Upvotes: 1

Views: 48

Answers (1)

Pepe
Pepe

Reputation: 6480

Updating to answer your question:

Try this:

WITH tbl2 AS (SELECT DISTINCT ON (Name) Name,Nickname FROM table2 ORDER BY Name)

SELECT tbl1.Name, tbl2.Nickname, tbl1.col2, tbl1.col3 FROM
table1 tbl1 INNER JOIN tbl2 ON tbl1.Name = tbl2.Name

Disclaimer: haven't tested this. Let me know if it works.

Upvotes: 2

Related Questions