본문 바로가기

PostgreSQL10

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.
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.
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.
C# Postgresql Helper Class C# 에서 사용할 수 있는 PostgreSQL Helper Class 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515.. 2019. 3. 21.
postgreSQL 외부접속 가능하게 하기 postgreSQL에서 외부접속 가능하게 하기 (macOS, homebrew) 아래의 설정 파일 2개를 수정해야 하는데 macOS에 homebrew로 postgreSQL을 설치 한 경우 그 위치는 이렇다. /usr/local/var/postgres - pg_hba.conf - postresql.conf 1. 먼저 postgresql.conf 파일을 열어서 listen_addresses 부분을 그림과 같이 '*'로 수정한 후에 저장한다.2. 다음 pg_hba.conf 파일을 열어서 그림의 빨간색 부분을 추가한다. host all all 0.0.0.0/0 password 3. 마지막으로 postgreSQL 서비스를 재시동 한다. $ brew services stop postgresql $ brew servi.. 2019. 3. 21.
brew 로 postgresql 설치 후 접속시 암호(pw) 묻지 않는 점 macOS에서 Homebrew로 postgreSQL을 설치한 후에 DB에 접속 시에 암호를 묻지 않는다.이유는 기본 설정이 pw를 묻지 않도록 되어 있기 때문이다. 이 설정 파일은 \usr\local\var\postgresql\pg_hba.conf 에 위치해있다.파일의 내용중에 trust 라고 되어 있는 부분을 password 로 변경해주어야 한다.설정에 대한 자세한 내용은 https://www.postgresql.org/docs/11/auth-pg-hba-conf.html 에서 확인할 수 있다. 설정이 변경되었으면 아래와 같이 서비스를 다시 실행해준다. -- 서비스 중지$ brew services stop postgresql -- 서비스 시작$ brew services start postgresql 2019. 3. 19.