programing

동일한 열에 대해 두 개 이상의 조건이 있는 카운트

goodjava 2023. 1. 14. 09:30

동일한 열에 대해 두 개 이상의 조건이 있는 카운트

2개의 특정 코스를 수강하는 sid(crn)의 수를 세고 싶습니다만, 그 sid는 표시할 수 있습니다만, 어떠한 이유로 카운트 할 수 없습니다.

crn = 12345 및 crn = 20976을 수강하는 학생 수를 표시하고 싶다.

여기 내 테이블이야

  sid  |  crn  | grade
 12321 | 12345 |   A
 12321 | 20976 |   B
 21008 | 12345 |   C
 21008 | 20976 |   A
 21008 | 28469 |   D
 21090 | 12345 |   C
 21090 | 20976 |   F

여기 동작하는 코드가 있습니다.

 select sid from takes where crn in (12345 , 20976 ) group by sid HAVING COUNT(*) = 2

여기 내가 시도했던 코드가 있다.

 select count(sid) from takes where crn in (12345 , 20976 ) group by sid HAVING COUNT(*) = 2

 select sid, count(sid) from takes where crn in ( '12345', '20976') GROUP By sid HAVING count(*) > 1

 select count(sid) from takes where (select sid from takes where crn in ( '12345', '20976') GROUP By sid HAVING count(*) > 1 )

 select sid, count(sid) from takes where (select sid from takes where crn in ( '12345', '20976') HAVING count(*) > 1 ) GROUP BY sid

그래서 제 질문은, 어떻게 하면 이걸 작동시킬 수 있을까요?제 말은, 어떤 코드가 그 숫자를 표시하고 표시할 수 있는 것을 세는 것이 얼마나 어려울까요?

서브쿼리가 필요합니다.

select count(*)
from (select sid
      from takes
      where crn in (12345 , 20976 )
      group by sid 
      having count(*) = 2
     ) s;

언급URL : https://stackoverflow.com/questions/50400989/counting-with-more-than-one-condition-for-same-column