Search code examples
sqlpostgresqlsubquerycommon-table-expression

Calling PostgreSQL function that returns table in Join


I have the following query:

WITH matching_data as (
 select * from consultants_matching_data_for_project(6001)
)

select 
  id,
  matching_data.city_matching

from 
  consultant_profiles
LEFT OUTER JOIN matching_data on matching_data.consultant_profile_id = consultant_profiles.id;

Is there any other way to join with matching_data result without using a WITH clause?


Solution

  • There was no need for the CTE anyway, nor a subquery. You can use the function just like a table:

    SELECT
      consultant_profiles.id,
      matching_data.city_matching
    FROM
      consultant_profiles
    LEFT JOIN consultants_matching_data_for_project(6001) AS matching_data
      ON matching_data.consultant_profile_id = consultant_profiles.id;