1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you mean ON instead of WHERE? Also, Imagine you have 1 row in table1 and 4 rows in table2, so will get more than one row when the condition table1.Name = table2.Name is satisfied.
So how would you know which record to select from table2?! Do you just want the first match? This might cause interesting behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.