Development Study/Backend

[SQL] 실습 정리

  • -
728x90

지난시간 복습

0.복습.1. Orders table의 orderdate를 월, 일 정보만 들고오기

select substring(date_format(orderdate, "%Y%m%d"), 5, 4)
from Orders

0.복습.2. 고객 별 월별 구매금액

select custid, substring(date_format(orderdate, "%Y%m%d"), 5, 2), sum(saleprice)
from Orders
group by custid, substring(date_format(orderdate, "%Y%m%d"), 5, 2)

0.복습.3. 만들어 둔 뷰 삭제하기

drop VIEW V_Orders;

이번 범위는 ~view까지..!

 

이번시간 실습 - csv파일 toad에 불러와 가공해보기

약 10,000개의 데이터를 가진 exel 파일을 불러와서 더 많은 데이터를 가공해보도록 하겠다

select *
from tmp;

1.tmp.1. 월별 총 구매금액

select substr(orddate, 5, 2), sum(prod_amt)
from tmp
group by substr(orddate, 5, 2)

hint: OO별, OO별 형식의 문제가 나온다면 GROUP BY를 이용하도록 하자

 

1.tmp.2. 고객별 월별 총 구매금액

select substr(orddate, 5, 2), custid , sum(prod_amt)
from tmp
group by substr(orddate, 5, 2), custid

1.tmp.3. 고객별 총 구매금액별로 출력

select custid , sum(prod_amt)
from tmp
group by custid

1.tmp.4. tmp table view로 만들어두기

create VIEW v_temp
as select custid, substr(orddate,1,6), sum(prod_amt)
from tmp
group by custid, substr(orddate,1,6)
order by 1,3 desc

 

1.tmp.5. 위 내용을 그대로 table로 만들기 - create table ( ) as select ( )

create table v_tempt
as select custid, substr(orddate,1,6), sum(prod_amt)
from tmp
group by custid, substr(orddate,1,6)
order by 1,3 desc

select * from v_tempt

2.cust_tmp.1. 성별, 연령대별, 월별, 총 구매금액

select ct.CLNT_GENDER, ct.CLNT_AGE, substr(tp.orddate, 5, 2), sum(tp.prod_amt)
from cust_tmp ct, tmp tp
where ct.custid = tp.custid
group by ct.CLNT_GENDER, ct.CLNT_AGE, substr(tp.orddate, 5, 2)
order by ct.CLNT_GENDER, ct.CLNT_AGE, sum(tp.prod_amt) desc;

2.tmp.2. 월별 구매고객 수

select substr(orddate, 5, 2), count(distinct(custid))
from tmp
group by substr(orddate, 5, 2)
order by 1;

2.tmp.3. prod_cnt가 null이면 0으로, 월별 인당 구매금액

select substr(orddate, 5, 2), ifnull(count(prod_cnt),0), sum(prod_amt), sum(prod_amt)/count(distinct custid)
from tmp
group by substr(orddate, 5, 2), custid, ifnull(prod_cnt, 0);

 

728x90
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.