show tables | count() | user_id | distinct(user_id) | enrolled_id | distinct(enrolled_id) |
|---|---|---|---|---|
| orders | 286 | 275 | - | - |
| users | 498 | 498 | - | - |
| enrolleds | 341 | 317 | 341 | 341 |
| enrolleds_detail | - | - | 21904 | 324 |
11명의 유저가 2번 결재(주문)했다.
select * from (select user_id, count(*) as cnt from orders group by user_id) a where cnt > 1count(*) > 1 이라고 하면, count(*)을 필드명으로 인식하지 못함 -> alias 를 붙여줌)users 테이블은 유저 아이디의 고유 정보를 나타낸다. (유저ID의 중복이 없다)
enrolleds(수강등록정보) 테이블은 enrolled 아이디의 고유 정보를 나타낸다.
enrolled 아이디는 유저(user_id) 당 1개 또는 2개일 수 있다. 왜냐하면 앱개발, 웹개발 모두 등록할 수 있으니까. (수강 등록 기준으로 enrolled_id 부여)
24명의 유저만 앱개발과 웹개발 모두 수강등록했다.
select * from (select user_id, count(*) as cnt from enrolleds group by user_id) a where cnt = 2두 과목 모두 수강등록한 24명의 유저 중 11명만 두 과목 모두 결재했다.
with table1 as (
select * from (
select user_id, count(*) as cnt_order from orders
group by user_id
) a
where cnt_order > 1
order by user_id
), table2 as (
select * from (
select user_id, count(*) as cnt_enrolled from enrolleds
group by user_id
) b
where cnt_enrolled > 1
order by user_id
)
select * from table1 c
left join table2 d on c.user_id = d.user_id 나머지 13명 중 3명만 두 과목 등록하고 1과목만 결재했다.
with table1 as (
select * from (
select user_id, count(*) as cnt_order from orders
group by user_id
) a
where a.cnt_order = 1
), table2 as (
select * from (
select user_id, count(*) as cnt_enrolled from enrolleds
group by user_id
) b
where cnt_enrolled > 1
)
select * from table2 d
left join table1 c on d.user_id = c.user_id
where c.user_id is not NULL 두 과목 모두 수강등록한 유저 중 나머지 10명은 한 번도 결재하지 않았다.
with table1 as (
select * from (
select user_id, count(*) as cnt_enrolled from enrolleds e
group by user_id
) a
where cnt_enrolled = 2
), table2 as (
select user_id, count(*) as cnt_order from orders o
group by user_id
)
select count(*) from table1 b
left join table2 c on b.user_id = c.user_id
where c.user_id is NULL결재를 한 사람은 모두 수강등록 했다. (그중에 2과목 모두 결재한 11명과, 2과목 등록하고 1과목만 결재한 3명이 포함되어 있다.)
with table1 as (
select * from (
select user_id, count(*) as cnt_enrolled from enrolleds e
group by user_id
) a
), table2 as (
select * from (
select user_id, count(*) as cnt_order from orders o
group by user_id
) b
)
select count(*) from table1 c
inner join table2 d on c.user_id = d.user_id
select count(*) from (
select user_id, count(*) as cnt_order from orders o
group by user_id
) b
그런데, 수강등록 하고 결재하지 않은 사람도 있다.
: 그중에서 수강등록한 두과목 모두 결재하지 않은 사람은 10명이고 한과목만 수강등록하고 결재하지 않은 사람은 32명이다. (orders 테이블에 아에 user_id가 없는 사람 = 결재한 적인 없는 사람 )
with table1 as (
select user_id, count(*) as cnt_enrolled from enrolleds e
group by user_id
), table2 as (
select user_id, count(*) as cnt_order from orders o
group by user_id
)
select count(*) from table1 c
left join table2 d on c.user_id = d.user_id
where d.user_id is NULLselect count(*) from(
select user_id, count(*) as cnt_enrolled from enrolleds
group by user_id
) a참고) 수강 등록한 개수 및 결재한 개수는 1아니면 2 뿐이다. 앱개발, 웹개발 두개 뿐이니까
enrolleds_detail에 포함되지 않은 enrolled_id가 17개 있다.
: 17개 enrolled_id = 16개 user_id
with table1 as (
select * from enrolleds e
), table2 as (
select distinct(enrolled_id) from enrolleds_detail ed
)
select distinct(a.user_id) from table1 a
left join table2 b on a.enrolled_id = b.enrolled_id
where b.enrolled_id is NULL
이 16개 user_id는 결재를 안한건가? or 결재하고 안듣는 건가?
참고) done이랑 seen이 모두 0인 enrolled_id는 없음 (enrolleds_detail에는 강의를 실제로 들은 사람만 있음)
with table2 as (
with table1 as (
select distinct(enrolled_id) from enrolleds_detail ed
)
select distinct(e.user_id) from enrolleds e
left join table1 a on e.enrolled_id = a.enrolled_id
where a.enrolled_id is NULL
)
select * from orders o
left join table2 b on o.user_id = b.user_id
where b.user_id is not NULLwith table3 as (
with table1 as (
select distinct(enrolled_id) from enrolleds_detail ed
)
select distinct(e.user_id) from enrolleds e
left join table1 a on e.enrolled_id = a.enrolled_id
where a.enrolled_id is NULL
), table4 as (
select distinct(e.user_id) from enrolleds e
left join orders o on e.user_id = o.user_id
where o.user_id is NULL
)
select * from table4 d
left join table3 c on d.user_id = c.user_id
where c.user_id is not NULL with table3 as (
with table1 as (
select distinct(enrolled_id) from enrolleds_detail ed
)
select distinct(e.user_id) from enrolleds e
left join table1 a on e.enrolled_id = a.enrolled_id
where a.enrolled_id is not NULL
), table4 as (
select distinct(e.user_id) from enrolleds e
left join orders o on e.user_id = o.user_id
where o.user_id is NULL
)
select c.user_id from table3 c
left join table4 d on c.user_id = d.user_id
where d.user_id is not NULL | count() | left join orders | left join users | left join enrolleds | left join enrolleds_detail |
|---|---|---|---|---|
| orders | - | 286 | 311 | - |
| users | 509 | - | 520 | - |
| enrolleds | 363 | 341 | - | 21921 |
| enrolleds_detail | - | - | 21904 | - |
결재한 user_id 에서 enrolled_id가 두개인 user_id 총 25개 추가 (311-286)
users 테이블의 user_id 에서 유저 당 추가 결재한 횟수만큼 추가해서 총 11개 추가(509-498=286-275=두과목 모두 결재한 유저 수)
(users 테이블은 user_id가 고유한 값)
users 테이블의 user_id 에서 두과목 모두 수강등록한 22명 추가(520-498)
select count(*) from users u
left join enrolleds e on u.user_id = e.user_id
where e.enrolled_id is NULL select * from (
select u.user_id, count(*) as cnt from users u
left join enrolleds e on u.user_id = e.user_id
group by u.user_id
) a
where cnt = 2
order by user_id;
```
select * from (
select user_id, count(*) as cnt from enrolleds e
group by user_id
) b
where cnt = 2
order by user_id;범인은.. 5f0ae408765dae0006002813, 5f11b9fe73570c0009f2498d !!!!!