- 배웠던 내용 총정리
-distinct : 중복 제거 (group by하지 않아도 연도별 중복 제거를 해서 볼 수 있음)
-where : 조건을 부여해서 원하는 데이터만 구해 옴 (필터처럼)
-연산
|
-집계함수, group by
|
-having, order by
-having은 select까지 해서 나온 결과값에 다시 필터 작업을 할 때 -having을 잘 쓰면 연산 비용을 줄일 수 있다.
-일차정렬, 이차정렬 가능 -정렬 후, limit으로 상위 몇개, 하위 몇개만 select하게 |
2) select * 3) where 4) group by -> select 앞에 붙이기 (암묵적인 국룰 !) 5) having 6) select 7) order by 8) limit |
-숫자, 문자열 함수
|
-날짜 함수
|
-조건분기
else end -> 연도별, 분기별 가입자수 조회할 때
|
-join
-user와 event(방문 로그) : 1대 N |
#cross join (join과 on을 사용하지 않음)
#왼쪽 오른쪽 테이블 다 붙인 다음에, id가 같지 않은 레코드는 다 날려서 join문과 같은 효과를 냄
select *
from user, order
where user.id = order.user_id
-union : 위 아래로 합치기
-with / sub query
-from에서 사용한 필드를 select절 안에서 서브쿼리로 활용할 수 있다 (user 10만, order 10만 레코드가 있으면... 10만 * 10만의 연산을 하게 됨 -> index최적화를 해서 연산 줄이기)
|
-rollup : 소계를 구함 (group by안에서 사용)
cf) grouping 함수 null값 확인할 때 사용
-window 함수
|
- 매출 분석 쿼리
#2022년 전체 주문건, 총 매출, 평균 매출
select
count(id) as total_order_count,
sum(sale_price) as total_sale_price,
avg(sale_price) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) = 2022
#2022년 월별 주문건, 총 매출, 평균 매출
select
extract(month from created_at) as month,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) = 2022
group by month
order by month
#2022연도까지의 모든연도의 주문문건수, 총 판매금액, 평균 판매금액을 조회
select
extract(month from created_at) as month,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) <= 2022
group by month
order by month
#2022년도와 2022년도 이하 모든연도의 월별 주문건수, 판매금액합계, 평균판매금액
with month_2022 as (
select
extract(month from created_at) as month,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) = 2022
group by month
order by month
), month_all as (
select
extract(month from created_at) as month,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) <= 2022
group by month
order by month
)
select
t1.month,
t1.total_order_count as order_count_2022,
t2.total_order_count as order_count_total,
t1.total_sale_price as sum_sale_price_2022,
t2.total_sale_price as sum_sale_price_total,
t1.avg_sale_price as avg_sale_price_2022,
t2.avg_sale_price as avg_sale_price_total
from month_2022 t1
join month_all t2 on t1.month = t2.month
order by month
#2022년 분기별(계절별) 주문건, 총 매출, 평균 매출
select
extract(quarter from created_at) as quarter,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) = 2022
group by quarter
order by quarter
#2022년도 이하 모든 연도의 분기별(계절별) 주문건, 총 매출, 평균 매출
select
extract(quarter from created_at) as quarter,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) <= 2022
group by quarter
order by quarter
#2022년도와 2022년도 이하 모든 연도의 분기별 주문건수, 판매금액합계, 평균판매금액
with quarter_2022 as (
select
extract(quarter from created_at) as quarter,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) = 2022
group by quarter
order by quarter
), quarter_all as (
select
extract(quarter from created_at) as quarter,
round(count(id), 2) as total_order_count,
round(sum(sale_price),2) as total_sale_price,
round(avg(sale_price),2) as avg_sale_price
from `thelook_ecommerce.order_items`
where extract(year from created_at) <= 2022
group by quarter
order by quarter
)
select
t1.quarter,
t1.total_order_count as order_count_2022,
t2.total_order_count as order_count_total,
t1.total_sale_price as sum_sale_price_2022,
t2.total_sale_price as sum_sale_price_total,
t1.avg_sale_price as avg_sale_price_2022,
t2.avg_sale_price as avg_sale_price_total
from quarter_2022 t1
join quarter_all t2 on t1.quarter = t2.quarter
order by quarter
- 실행계획 기능
'AI School 8기 (LIKELION, 22.12~23.05)' 카테고리의 다른 글
[AI School-TIL] SQL(BigQuery) 6일차_23.02.03 (0) | 2024.12.12 |
---|---|
[AI School-TIL] SQL(BigQuery) 5일차_23.01.27 (1) | 2024.12.12 |
[AI School-TIL] SQL(BigQuery) 4일차_23.01.26 (0) | 2024.12.12 |
[AI School-TIL] SQL(BigQuery) 3일차_23.01.25 (2) | 2024.12.11 |
[AI School-TIL] SQL(BigQuery) 2일차_23.01.20 (0) | 2024.12.11 |