-1

My question is how can I get data of column1 depending on the column2.

This is a small example of data that I have stored.

create table Student_subject
(
    Student varchar(20),
    Subject varchar(20)
)

insert into Student_subject 
values ('Rohan', 'Maths'),
       ('Rohan', 'Science'),
       ('Rohan', 'Language1'),
       ('RAJ'  , 'Maths'),
       ('RAJ'  , 'Science'),
       ('RAJ'  , 'Language1'),
       ('SAM'  , 'Maths'),
       ('SAM'  , 'Language1')

I want to get the distinct row of Students that are studying all three subjects ('Maths', 'Science', 'Language1'). In this case the result will be Rohan and Raj. Please also advise a query on how to achieve the below.

If subjects are specified:

  1. List of students that are studying { Maths, Science, Language1 }
  2. List of students that are studying { Maths, Science }
Charlieface
  • 52,284
  • 6
  • 19
  • 43
MShaikh
  • 1
  • 1
  • Please don't use HTML to format your code; Markdown is fully supported here, and you can use code fences for code blocks – Thom A Oct 20 '21 at 09:22

4 Answers4

0
SELECT S.STUDENT
FROM Student_subject S
GROUP BY S.Student
HAVING COUNT(DISTINCT S.Subject)=
  (SELECT COUNT (DISTINCT Subject) FROM Student_subject )
Sergey
  • 4,719
  • 1
  • 6
  • 11
  • That is helpful and what if I want to specify the subjects Maths and Language1 or Maths and Science or maybe Maths, Science and Language1 – MShaikh Oct 20 '21 at 09:32
  • Please try this one SELECT S.STUDENT FROM Student_subject S WHERE S.Subject IN('Maths','Language1' ) GROUP BY S.Student HAVING COUNT(DISTINCT S.Subject)=(SELECT COUNT (DISTINCT Subject) FROM Student_subject WHERE Subject IN('Maths','Language1' )) – Sergey Oct 20 '21 at 09:45
0

You can count distinct subjects for a student.

Select Student
From Student_subject
Group by Student
Having Count(Distinct [Subject])=3

or you can use string_agg function to get a list of target subjects.

Select Student
From Student_subject
Group by Student
Having string_agg([Subject],',') Within Group (Order by [Subject])='Language1,Maths,Science'
Order by Student
Anton Grig
  • 1,640
  • 7
  • 11
0

try this, maybe it was helpful please consider that if you want to make your query dynamic with multiple inputs you can use json parameter

here is a sample for json input


DECLARE @test NVARCHAR(MAX)='["Language1","Maths"]'
SELECT Student,COUNT(1)
FROM Student_subject
WHERE Subject IN 
(
SELECT Value
FROM OPENJSON(@test,'$')
)
GROUP BY Student
HAVING COUNT(1)>=(SELECT COUNT(Value)
FROM OPENJSON(@test,'$'))
A30
  • 11
  • 3
-1

SELECT student FROM student_subject GROUP BY S.Student HAVING COUNT(DISTINCT S.Subject)= (SELECT COUNT (DISTINCT Subject) FROM Student_subject )

Sucre
  • 3
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 20 '21 at 09:27