I have a one to many relationship mapping table in postgres and the tables are as below:
Students
id | Name |
---|---|
1 | Shibbir |
2 | Ahmed |
3 | John |
Courses
id | Name |
---|---|
1 | Data Structure |
2 | Algorithms |
3 | Math |
student_course_mapping
studentId | courseId |
---|---|
1 | 1 |
1 | 2 |
1 | 3 |
2 | 2 |
2 | 3 |
3 | 1 |
3 | 2 |
Given that
a student must have unique course combinations:
if student 1 have the course 1,2,3 then none of the other students can have same course combinations, thus student 2 has to to take course combination 2,3 and then finally student 3 has to take course combination 1,2.
Finally my question is:
if I have a combination of course ids, then how to write the query to get the unique student id from student_course_mapping table who has exactly the same combination of courses.
example :
input course ids: 1,2,3 then output should be 1
input course ids: 2,3 then output should be 2
input course ids: 1,2 then output should be 3
It would be great to be able to write a single query that gives the desired result.
2
Answers
To get the values position insensitive solution you can use arrays functions and operators – the
ARRAY_AGG
function to create arrays ofcourseid
values for eachstudentid
, the@>
operator to check ifcourseid
array of a student contains the array of the inputcourseid
, theARRAY_LENGTH
to check if arrays have equal length.Output
db<>fiddle here
In this case is necessary to create a temporary table because it’s not possible to include aggregations in where clause
then
Since in your sample data doesn’t occur the case that you expect, I added an extra row in student_course_mapping to exemplify when two students have same combination of courses.
To test https://www.db-fiddle.com/f/tEWNzwHX2YhtuRhoohyh4B/0