How do I create a PostgreSQL function (that can be...
# help
n
How do I create a PostgreSQL function (that can be called using
supabase.rpc
) that will run the following query?
Copy code
pgsql
select distinct on (view_users.id) view_users.*
from relation_meeting_people relation_meeting_people1
join relation_meeting_people relation_meeting_people2
on relation_meeting_people1.meeting = relation_meeting_people2.meeting
join view_users
on relation_meeting_people2.user = view_users.id
where relation_meeting_people1.user = 'p5W8EB2YKReYAJz334UGMDi6m9H2';
And will replace
'p5W8EB2YKReYAJz334UGMDi6m9H2'
with the
user_id
passed to the function?
Solution:
Copy code
pgsql
create or replace function meeting_users(user_id text)
returns table (like view_users)
as $$
  select distinct on (view_users.id) view_users.*
  from relation_meeting_people relation_meeting_people1
  join relation_meeting_people relation_meeting_people2
  on relation_meeting_people1.meeting = relation_meeting_people2.meeting
  join view_users
  on relation_meeting_people2.user = view_users.id
  where relation_meeting_people1.user = user_id;
$$
language sql stable;