본문 바로가기
참톨공부

[SQL] 3주차 개발일지

by 참도토리 2021. 12. 20.

3-5 퀴즈
select o.payment_method, round(avg(pu.point),0) as avg_point from point_users pu
inner join orders o 
on pu.user_id = o.user_id 
group by o.payment_method

select name , count(*) as cnt_name from enrolleds e
inner join users u 
on e.user_id = u.user_id 
where e.is_registered = 0
group by u.name
order by cnt_name desc

select c.course_id, c.title, count(*) as cnt from courses c 
inner join enrolleds e 
on c.course_id = e.course_id
where e.is_registered = 0
group by c.course_id

select c1.title, c2.week, count(*) as cnt from courses c1
inner join checkins c2
on c1.course_id = c2.course_id
group by c1.title, c2.week
order by c1.title, c2.week 

select c1.title, c2.week, count(*) as cnt from courses c1
inner join checkins c2 on c1.course_id = c2.course_id
inner join orders o on c2.user_id = o.user_id
where o.created_at >= ‘2020-08-01’
group by c1.title, c2.week
order by c1.title, c2.week

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’

(
select ‘7월’ as month, c1.title, c2.week, count(*) as cnt from courses c1
inner join checkins c2 on c1.course_id = c2.course_id
inner join orders o on c2.user_id = o.user_id
where o.created_at >= ‘2020-08-01’
group by c1.title, c2.week
)
union all
(
select ‘8월’ as month, c1.title, c2.week, count(*) as cnt from courses c1
inner join checkins c2 on c1.course_id = c2.course_id
inner join orders o on c2.user_id = o.user_id
where o.created_at >= ‘2020-08-01’
group by c1.title, c2.week

3주차 숙제
select e.enrolled_id, e.user_id, count(*) as max_count from enrolleds e
inner join enrolleds_detail ed on e.enrolled_id = ed.enrolled_id 
where ed.done = 1
group by e.enrolled_id 
order by max_count desc


알아둘 것

union all에서는 order by 가 안먹는다

728x90
반응형

댓글