programing

오라클에서 두 번 이상 발생한 기록을 검색하는 방법은 무엇입니까?

elecom 2023. 7. 22. 09:23
반응형

오라클에서 두 번 이상 발생한 기록을 검색하는 방법은 무엇입니까?

이 테이블이 있습니다.

create table student (
   stu_id int,
   s_name nvarchar(max),
   s_subject nvarchar(max),
)

그리고 이것은 데이터로서.

insert into student values(123,'pammy','English');
insert into student values(123,'pammy','Maths');
insert into student values(123,'pammy','Chemistry');
insert into student values(124,'watts','Biology');
insert into student values(125,'Tom','Physics');
insert into student values(125,'Tom','Computer';
insert into student values(125,'Tom','ED';

그래서 저는 두 번 이상 발생한 기록을 찾고 싶었습니다.내 코드는

select stu_id,s_Name 
from student 
group by stu_id,s_Name 
having count(stu_id) >2 ;

결과는 완벽했습니다.

하지만 내가 원할 때s_subject또한 행이 선택되지 않았다고 표시됩니다.왜 그런지 모르겠어요.

select stu_id,s_Name,s_subject 
from student 
group by stu_id,s_Name,s_subject 
having count(stu_id) >2 ;

당신의 학생들 중 어느 누구도 과목당 하나 이상의 기록을 가지고 있지 않기 때문입니다.

select stu_id,s_Name,s_subject 
from student 
group by stu_id,s_Name,s_subject 
having count(stu_id) >2 ;

이 코드는 동일한 학생 ID, 이름 및 제목을 가진 두 번 이상 발생하는 레코드를 요청합니다.샘플의 레코드 중 이 값을 충족하는 레코드가 없습니다.

하지만, 만약 여러분이 실제로 원하는 것이 두 개 이상의 수업을 듣는 학생의 ID, 이름, 과목이라면, 이것은 꽤 쉽게 성취될 수 있습니다.

초기 SQL의 약간 수정된 버전을 필터로 사용하면 다음과 같은 이점을 얻을 수 있습니다.

select stu_id, name, subject
from student
where stu_id in (   select stu_id 
                    from student 
                    group by stu_id 
                    having count(stu_id) >2 );

이게 도움이 되길 바랍니다.

테이블에 있는 모든 열을 기준으로 그룹화하면 고유한 행(빈도 행이 하나인 레코드)이 생성됩니다.이미 2개 이상의 행을 선택했으므로 빈도가 2인 레코드는 없습니다.count=1을 사용할 경우 count=1을 사용하는 모든 행이 표시됩니다.

select stu_id,s_Name,s_subject 
from student 
group by stu_id,s_Name,s_subject
having count(stu_id) =1 ;

출력은 다음과 같습니다.

stu_id      s_Name      s_subject
   ----------- -------------
123         pammy       Chemistry
123         pammy       English
123         pammy       Maths
124         watts       Biology
125         Tom         Computer
125         Tom         ED
125         Tom         Physics

언급URL : https://stackoverflow.com/questions/9582538/how-to-retrieve-records-which-occurred-more-than-twice-in-oracle

반응형