Skip to content
Advertisement

How to get value from a query of another table to create a new column (postgresql)

I am new to postgres and I want to be able to set value to Y if order (order table) is a first month order (first month order table)

first month order table is as per below. It will only show the order placed by user the first time in the month:

order table is as per below. It shows all the order records:

To get the first month order column in the order table, I tried using case as below. But then it will give the error more than one row returned by a subquery.

I also tried using count but then i understand that this is quite inefficient and overworks the database i think.

How can I determine if the order is first month order and set the value as Y for every order in the order table efficiently?

Advertisement

Answer

Use the left join as follows:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement