커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
ratio관련 질문
[왕초보] 엑셀보다 쉬운, SQL v3
4주차
북마크
장*은
댓글
3
추천
0
조회수
15
조회수
15
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


안녕하세요 select에서 ratio를 구할 때 count를 언제 쓰고 언제 안 쓰는지 모르겠어서 처음으로 질문 남깁니다!

SELECT count(pu.point_user_id) as pnt_user_cnt,

count(u.user_id) as tot_user_cnt,

round(count(pu.point_user_id)/count(u.user_id),2) as ratio

from users u

left join point_users pu on u.user_id = pu.user_id

WHERE u.created_at BETWEEN '2020-07-10' and '2020-07-20'

이 쿼리에서는 count를 넣어야 ratio가 구해지고,


select a.course_id, b.cnt_checkins, a.cnt_total, (b.cnt_checkins/a.cnt_total) as ratio from

(

select course_id, count(*) as cnt_total from orders

group by course_id

) a

inner join (

select course_id, count(distinct(user_id)) as cnt_checkins from checkins

group by course_id

) b

on a.course_id = b.course_id

여기선 ratio를 구할 때 count없이 구했는데, 차이점을 잘 모르겠습니다.








취소
 공유
취소
댓글 0
댓글 알림
나의얼굴