본문 바로가기

분석/데이터분석42

with statement in SQL with 문을 사용하면 SQL 문장을 매우 직관적으로 사용할 수 있는데, 다음과 같이 몇 가지 형태로 사용할 수 있다. 1. sub query를 with 문 으로 with list1 as ( select id, area from tbl_address where area = 'seoul' ), list2 as ( select subject, avg(score) from tbl_score where id in (select id from list1) group by subject ) select * from list2; 물론 위의 쿼리는 with문을 사용하지 않고 처리할 수도 있지만, with 문안의 가상테이블에서 다른 가상테이블을 조건으로 사용할 수도 있으며 복잡한 쿼리를 단순하게 직관적으로 표현할 수 있.. 2020. 1. 29.
How To Calculate Cohort Retention in SQL ‍Losing users sucks. Losing customers really sucks. If you’re a startup, you know that Retention is King. You should always be measuring and improving your user retention, so you can keep more users over time. In this post, we’ll show you how to calculate user retention on your own data in SQL. Defining retention If Gloria used the product on Monday and used the product again on Tuesday, she is .. 2020. 1. 7.
RFM Analysis (RFM 분석) R : recency (최근 구매일) F : frequency (구매 횟수) M : monetary (구매 금액 합계) 에 따른 고객군 구분 2019. 9. 24.
PostgreSQL : Windows Function (윈도우함수 정리) Windows Function을 사용하면 group by를 사용하지 않고도 SQL의 aggregate 함수를 사용할 수 있다. -- 원래 데이터 SELECT country, month, goalamount FROM TARGET order by month, country TARGET 이라는 테이블에 아래와 같이 국가별, 월별 목표금액이 있는 테이블이 있다고 가정을 하고... * 편의상 모든 결과는 20 row 까지만 캡춰를 하였습니다. 1. 집계 함수 -- 집계 함수 SELECT country, month, goalamount , avg(goalamount) over() as avg_amount , avg(goalamount) over(partition by month) as avg_month FROM T.. 2019. 9. 18.
PostgreSQL : psql 명령어 정리 Postgres의 command line 인터페이스인 psql 에서 사용하는 명령어를 정리해본다. ● 사용자 조회 # \du ● 데이터베이스 조회 # \l ● 데이터베이스 접속 (변경) # \c db_name ● 스키마 조회 # \dn ● 스키마 접속 (변경) # set search_path to schema_name; ● 테이블 조회 # \dt ● function 조회 # \df 2019. 9. 6.
PostgreSQL : generate series of date (일련의 날짜 만들기) PostgreSQL에서는 generate_series 라는 함수를 이용하면 일련의 숫자를 만들 수 있다. select generate_series(0, 364) as idx 위의 커리문장은 0부터 364까지의 숫자 레코드셋을 만들어준다. 여기서 응용을 하면 날짜의 덧셈과 cross join 기능을 이용하면 날짜 series를 만들 수 있다. select a.date + b.idx as date from ( select '2019-01-01'::date as date ) a cross join ( select generate_series(0, 364) as idx ) b 2019-01-01 2019-01-02 2019-01-03 2019-01-04 2019-01-05 2019-01-06 2019-01-07.. 2019. 9. 6.
Mysql에서 rownum 과 그룹별 rownum 이런 table이 있다고 가정하자 name : scores column : class, name, score class (반), name(학생이름), score(점수) 하고자 하는 작업은 각 반별로 상위 점수 3명을 구하는 것이다. 1. rownum mysql에서는 rownum을 제공하지 않기 때문에 다음과 같이 해서 구할 수 있다. select class, name, score, @rownum:=@rownum+1 as rownum from scores, (select @rownum:=0) r 2. rownum by group : 그룹별로 rownum을 만들어 보자 -- group 별 rownum select class, name, score , case when @grp = class then @row.. 2019. 8. 8.
PostgreSQL - DATEDIFF - Datetime Difference in Seconds, Days, Months, Weeks etc You can use various datetime expressions or a user-defined DATEDIFF function (UDF) to calculate the difference between 2 datetime values in seconds, minutes, hours, days, weeks, months and years in PostgreSQL. Overview PostgreSQL does not provide DATEDIFF function similar to SQL Server DATEDIFF, but you can use various expressions or UDF to get the same results. SQL Server and Sybase Postgresql .. 2019. 6. 14.
PostgreSQL : Schema Backup PostgreSQL에서는 다른 database의 데이터를 쿼리할 수가 없다. 그런 이유로 개발자들은 DB Link를 설정하거나 또는 schema를 생성한다. 이 글에서는 특정 schema만 파일로 백업하는 것을 설명한다. $ pg_dump --schema=schema_name db_name > backupfile.sql 이 파일에서 다시 복원하려면 $ psql -d db_name -h localhost -U user_name < backupfile.sql psql 커멘드라인에서 스키마를 변경할때 # set search_path to schema_name psql 커멘드라인에서 스키마를 삭제할때 # drop schema schema_name cascade psql 커멘드라인에서 스키마를 소유자를 변경할때 .. 2019. 5. 14.