amulya349
amulya349

Reputation: 1238

How to create a view with inner join with variables in PostgreSQL?

I am using PostgreSQL DB. I need to create a view which consists of a query with joins on multiple tables, but I am stuck at a point. Please read the below table definition and the Query which I have created.

Tables:

Students
---------
id -> UUID
full_name -> VARCHAR
email_id -> VARCHAR
location -> VARCHAR
created_at -> timestamp
modified_at -> timestamp

Subjects
--------
id -> UUID
title -> VARCHAR
classroom -> VARCHAR
created_at -> TIMESTAMP


Applications
------------
id -> UUID
student_id -> FOREIGN KEY (Students.id)
subject_id -> FOREIGN KEY (Subjects.id)
verified -> BOOLEAN
application_response -> JSON
status_id -> FOREIGN KEY (ApplicationStatus.id)
created_at -> TIMESTAMP

ApplicationStatus
-----------------
id -> UUID
application_status -> VARCHAR
category -> VARCHAR

Scores
-------
student_id -> FOREIGN KEY (Students.id)
subject_id -> FOREIGN KEY (Subjects.id)
score -> NUMERIC

Following is the SQL query which I have created:

create or replace view testing_list_view as 
select c.id as student_id,
a.subject_id as subject_uid,
c.full_name, 
c.email_id,  
c.created_at, 
p.score, 
s.application_status,
a.verified,
a.application_response
from students c
inner join scores p
on p.student_id=c.id and p.subject_id = 'ff342ada-f5gb-44fb-bdth-44e3n59f5448'
inner join applications a
on a.student_id = c.id and a.subject_id= 'ff342ada-f5gb-44fb-bdth-44e3n59f5448'
inner join applicationstatus s 
on s.id = a.status_id
where c.id in 
(select student_id from applications where subject_id='ff342ada-f5gb-44fb-bdth-44e3n59f5448')

Here, I am getting the list of students for the given subject_id along with the scores, application_status and some other fields for which I need to do joins.

My requirement is to make a view for this query so that I pass only the subject_id to the view (select * from testing_list_view where subject_uid='ff342ada-f5gb-44fb-bdth-44e3n59f5448') and I will get a list of the students who have applied to the given subject. But, I am stuck here as in the above join query, subject_id is required in multiple times in the join clauses and is hard-coded. So, I am unable to make it a view.

I was thinking if some kind of variable exists using which, in the join clauses, I will use the variable in the required clauses and which querying the view I will pass it the value (subject_id).

Please let me know if any more clarification is needed.

Upvotes: 1

Views: 9879

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246238

You should define the view without the WHERE clause and all the other restrictions on subject_id:

create or replace view testing_list_view as 
select c.id as student_id,
       a.subject_id as subject_uid,
       c.full_name, 
       c.email_id,  
       c.created_at, 
       p.score, 
       s.application_status,
       a.verified,
       a.application_response
from students c
   inner join scores p
      on p.student_id=c.id
   inner join applications a
      on a.student_id = c.id and a.subject_id = p.subject_id
   inner join applicationstatus s 
      on s.id = a.status_id;

You specify the condition when you use the view, e.g.

SELECT * FROM testing_list_view
WHERE subject_uid = 'ff342ada-f5gb-44fb-bdth-44e3n59f5448';

Upvotes: 2

Related Questions