주어진 값이있는 레코드의 백분율을 알고 싶습니다. 여기서 백분율은 값과 일치하는 레코드 수를 총 레코드 수로 나눈 값으로 정의됩니다. 즉, 100 개의 레코드가 있고 그 중 10 개는 student_id에 대해 null 값을 갖고 20은 999999의 값을 갖는 경우 percent_999999는 20 % 여야합니다. AVG 기능을 사용하여이를 확인할 수 있습니까?
옵션 1:
SELECT year, college_name,
sum(case when student_id IN ('999999999') then 1 else 0 end) as count_id_999999999,
count_id_999999999/total_id as percent_id_999999999,
sum(case when student_id IS NULL then 1 else 0 end) as count_id_NULL,
count_id_NULL/total_id as percent_id_NULL
count(*) as total_id
FROM enrolment_data ed
GROUP BY year, college_name
ORDER BY year, college_name;
옵션 2 :
SELECT year, college_name,
sum(case when student_id IN ('999999999') then 1 else 0 end) as count_id_999999999,
avg(case when student_id IN ('999999999') then 1.0 else 0 end) as percent_id_999999999,
sum(case when student_id IS NULL then 1 else 0 end) as count_id_NULL,
avg(case when student_id IS NULL then 1.0 else 0 end) as percent_id_NULL
count(*) as total_id
FROM enrolment_data ed
GROUP BY year, college_name
ORDER BY year, college_name;
100 개의 레코드, 20 개의 999999999s, 10 개의 null 및 70 개의 1이있는 유사한 테이블을 만들었습니다. 이것은 SQL Server에서 나를 위해 일했습니다.
select count(*), StudentID
from ScratchTbl
group by StudentID;
(No column name) StudentID
10 NULL
70 1
20 999999999
select avg(case when StudentID = '999999999' then 1.0 else 0.0 end) as 'pct_9s',
sum(case when StudentID = '999999999' then 1 else 0 end) as 'count_9s',
avg(case when StudentID is null then 1.0 else 0.0 end) as 'pct_null',
sum(case when StudentID is null then 1 else 0 end) as 'count_null'
from ScratchTbl
pct_9s count_9s pct_null count_null
0.200000 20 0.100000 10
group by 절을 사용하면 문제가 발생할 수 있다고 생각합니다. where 절을 사용하여 특정 연도 / 대학을 선택하고 (그리고 한 줄로 그룹을 제거하고) 예상 한 결과를 얻을 수 있는지 확인합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다