Skip to content
Advertisement

postgresql combine 2 select from 2 different tables

Hello i need to run a query with 2 select statements, one with an avg calculation and grouped by name and the other select, looking for that name in other table, and get some columns and do a JOIN to merge both and only left one column “name” on it.

The first table its this:

The second table its like this:

The final query should return something like this:

I know this query should be something like this:

but doesnt work, thanks for your heads up and help me to debug this

Advertisement

Answer

The second subquery is invalid as digit and currrency need to be part of the group by list (except if “Name” is a primary key). Anyway distinct on ("Name") will extract one record per name which I suppose is what you need; You can control which record to be picked for each name by adding an order by clause with a list of expressions that starts with “Name”.
cross join will yield 4 records’ result not 2. I suppose that you need an inner join.

However this would be the same as

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