데이터분석 기록일지

문제풀이/스파르타 - sql 문제

[SQL] 데이터와 친해지는 SQL | 2회차 숙제

야하루 2024. 6. 27. 18:42

- 문제에 사용되는 table 구조

 

-  table 칼럼 설명

구분
상세
schema
logid
로그id
int
ip_addr
ip주소
string
date
날짜, yyyy-mm-dd
string
game_account_id
게임계정id
string
game_actor_id
게임캐릭터id
int
level
현재레벨
int
exp
현재경험치
int
serverno
서버넘버
int
zone_id
지역넘버
int
etc_num1
파티id
int
etc_num2
파티원수
int
etc_str1
아이템 획득경로
string
etc_num3
아이템 획득량
int
etc_str2
아이템 이름
string

 

 

 

1.  group by 절을 사용하여, 서버별 게임캐릭터id수(중복값 허용x)와 평균 경험치를 추출해주세요.

 

select serverno, count(distinct(game_actor_id)) count_id, avg(exp) avg_exp
from users
group by serverno
order by serverno

 

 

 

2. group by 와 having 절을 사용하여, 날짜 별(yyyy-mm-dd) 게임캐릭터id수(중복값 허용x)를 구하고, 그 값이 10개를 초과하는 경우를 추출해주세요.

 

select date, count(distinct(game_actor_id))
from users
group by date
having count(distinct(game_actor_id)) > 10

 

 

 

3. 위와 같은 문제를 having 이 아닌 인라인 뷰 subquery를 사용하여, 추출해주세요.

 

select a.date, a.count_id
from
(
select date, count(distinct(game_actor_id)) as count_id
from users
group by date
) a
where a.count_id > 10

 

 

2회차 숙제도 간단히 끝