题解 | #统计作答次数#
统计作答次数
https://www.nowcoder.com/practice/45a87639110841b6950ef6a12d20175f
# 有一个试卷作答记录表exam_record,请从中统计出总作答次数total_pv、
# 试卷已完成作答数complete_pv、已完成的试卷数complete_exam_cnt。
# 写法一:不用case when
# select count(id),count(score)
# ,(
# select count(distinct exam_id)
# from exam_record
# ) as complete_exam_cnt
# from exam_record
# 写法二:case when
select count(id)
,count(case when score is not null then 1 else null end )
,count(distinct CASE when score is not null then exam_id else null end)
from exam_record
# 试卷已完成作答数complete_pv、已完成的试卷数complete_exam_cnt。
# 写法一:不用case when
# select count(id),count(score)
# ,(
# select count(distinct exam_id)
# from exam_record
# ) as complete_exam_cnt
# from exam_record
# 写法二:case when
select count(id)
,count(case when score is not null then 1 else null end )
,count(distinct CASE when score is not null then exam_id else null end)
from exam_record