1

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.

3
  • 1
    Views can't have parameters, but you can create a SQL function (returns table (...) ) that takes a parameter as an input and simply returns the result of the query Commented Feb 28, 2019 at 11:01
  • 1
    look at answers here - stackoverflow.com/questions/11401749/… Commented Feb 28, 2019 at 11:04
  • @JosMac Looks relevant to my problem. Let me try the solution of that question. Commented Feb 28, 2019 at 12:10

1 Answer 1

2

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';
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer. I could not understand the answer. Can you please elaborate?
I have elaborated by adding the view definition I envision.
When I ran the query you have given, I got an error - SQL Error [42702]: ERROR: column reference "subject_id" is ambiguous. It is because subject_id column is a foreign key in both applications and scores tables. That's why it is giving that error.
Which query? The column subject_id in the view definition is qualified and cannot be ambiguous.
There was a mistake in my query that's why error is coming. Well, the query ran successfully. But, the results are not correct. Because, When I ran my original query (in the question) it is giving a count of 215 (which is correct). But, when I created a view without subject_id and which querying, I used subject_id, it gave me a result count of 2150.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.