분석함수를 나타내는거 over()

partition by는 계산 대상 그룹을 지정하고.. 라는데 무슨 말인지 이해가 안된다..

[링크 : http://www.gurubee.net/lecture/2671]

[링크 : https://syoons.tistory.com/8]

[링크 : https://brownbears.tistory.com/310]

 

곰곰히 생각해보니.. 여러개 쿼리로 일일이 조건을 줘서 통계를 내야 할 걸

한줄에 편리하게 하는 것일 뿐인가?

select *
from(
   select code, date, account, upd_date,

   max(upd_date) over (partition by code, date) as maxdate from hoge

) t1
where t1.upd_date = t1.maxdate;

[링크 : https://javafactory.tistory.com/1343]

 

+

편하게 쓰려고 만들었다 라는 말이 맞는게

group by는 전체 쿼리에 적용

partition by는 window function 으로 작동

[링크 : https://stackoverflow.com/questions/2404565/sql-server-difference-between-partition-by-and-group-by]

Posted by 구차니

좀 대량으로 지울일이 있어서 pgadmin으로 하려니 일일이 클릭해야 해서 대략 빡침 시전

그러다가 psql 가서 하려니..

크아아 public schema가 아니라 바로 안나오네?!?!

 

그래서 고민 때리다 검색해보니

[링크 : https://stackoverflow.com/questions/4202135/how-to-drop-multiple-tables-in-postgresql-using-a-wildcard]

 

pg_tables와 pg_sequences에서 테이블 명을 검색해서 뽑아낼수 있어

그걸 이용해서 drop table, drop sequence를 해버리니 편하네

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

array_agg 빈 배열을 누적할 수 없습니다.  (0) 2019.11.28
sql over() partition by  (0) 2019.11.28
pgcrypt encrypt() aes cbc ecb 그리고 padding  (0) 2019.11.25
sql TO_CHAR()  (0) 2019.11.23
sql where like %와 _  (0) 2019.11.22
Posted by 구차니

일단~은

AES/CBC/PKCS5PADDING이 기본이라고 보면 될 듯?

 

Encrypt/decrypt data using the cipher method specified by type. The syntax of the type string is:

where algorithm is one of:

  • bf — Blowfish

  • aes — AES (Rijndael-128, -192 or -256)

and mode is one of:

  • cbc — next block depends on previous (default)

  • ecb — each block is encrypted separately (for testing only)

and padding is one of:

  • pkcs — data may be any length (default)

  • none — data must be multiple of cipher block size

[링크 : https://www.postgresql.org/docs/9.6/pgcrypto.html]

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

sql over() partition by  (0) 2019.11.28
pg_tables, pg_sequences  (0) 2019.11.28
sql TO_CHAR()  (0) 2019.11.23
sql where like %와 _  (0) 2019.11.22
split_part() 에서 나눠지지 않을 경우 없는 막기  (0) 2019.11.15
Posted by 구차니

어떻게 보면.. sprintf 역활을 하는 함수라고 하면 되려나?

특정 자릿수로 제한하거나 공백에 0을 채우거나 할때

 

SELECT TO_CHAR(value, '00000')

이렇게 하면 value가 5자리를 넘지 않을 경우

00001~99999 까지 자릿수 유지하면서 출력한다.

[링크 : https://www.postgresql.org/docs/9.3/functions-formatting.html]

Posted by 구차니

%는 * 의미

_는 ? 의미 로 사용이 가능하다.

[링크 : https://www.w3schools.com/sql/sql_wildcards.asp]

Posted by 구차니

1123.0

이라는 숫자가 있고

1123 이 있는데

split_part(num, '.', 2)를 하면 1123은 0으로 나누어지지만

1123은 ''으로 나오기에 미리 값을 확인하고 null로 case 문 처리 해야 할 듯

 

ASE WHEN version LIKE '%-%'
     THEN SPLIT_PART(version, '-', 2)::int
     ELSE 0 END

[링크 : https://stackoverflow.com/questions/45766644/substituting-value-in-empty-field-after-using-split-part]

'프로그램 사용 > postgreSQL' 카테고리의 다른 글

sql TO_CHAR()  (0) 2019.11.23
sql where like %와 _  (0) 2019.11.22
postgres table schema comment 달기  (0) 2019.11.13
with in sql  (0) 2019.11.11
where not in 에서는 null 값을 조심하자  (0) 2019.11.11
Posted by 구차니

pgadmin 프로그램 버전에서 테이블 클릭시 create table 명령과 함께 구조가 나오는데 거기에 주석을 다는 방법

웹 버전의 pgadmin(윈도우) 에서는 comment를 어떻게 볼수 있는지 모르겠네..

 

 

아무튼 결론

테이블을 만들고 나서 comment on column table.column_name is 'comment' 식으로 달면된다.

create table session_log 

   userid int not null, 
   phonenumber int
); 

comment on column session_log.userid is 'The user ID';
comment on column session_log.phonenumber is 'The phone number including the area code';

 

[링크 : http://lnk2580.blogspot.com/2014/12/db-postgresql-comment.html]

[링크 : https://stackoverflow.com/.../adding-comment-to-column-when-i-create-table-in-postgresql]

 

+

코멘트 없애는건 빈 코멘트를 쓰면된다.

Only one comment string is stored for each object, so to modify a comment, issue a new COMMENT command for the same object. To remove a comment, write NULL in place of the text string. Comments are automatically dropped when their object is dropped.

 

COMMENT ON COLUMN my_table.my_column IS 'Employee ID number';

[링크 : https://www.postgresql.org/docs/9.1/sql-comment.html]

[링크 : http://www.dbatodba.com/db2/db2-comment/]

 

+

테이블 컬럼의 코멘트는 웹 버전보다 리눅스 어플리케이션 버전이 더 보기 편하네?

[링크 : https://dataedo.com/kb/tools/pgadmin/how-to-view-and-edit-table-and-column-comments]

Posted by 구차니

걍.. with 변수명 as (select ...)

하고 쓰면 되네? 일종의 임시 테이블로 쓰이는구나..

[링크 : https://thebook.io/006696/part01/ch07/02/]

Posted by 구차니

null이 하나라도 있으면

and value != NULL 조건이 되어서

정상적으로 걸러지지 않는 듯

[링크 : https://doorbw.tistory.com/222]

Posted by 구차니

값을 추적하다 보니 몇개가 사라져서 역으로 찾다보니

unnest() 안에 들어가는 항목이 null 일 경우에는 (empty array가 아닌 null)

아예 해당 항목 자체가 select 되지 않는 문제가 존재한다.

 

해결법이 맞는진 모르겠지만..

null일 조건 비교해서 두번 선택해야 할지도?

(한번에 선택하는 법이 있으면 좋겠네)..

 

값을 비교해서 unnest 하거나, lateral join을 쓴다는데

lateral join을 아직 모르니.. 일단 값 비교 하는걸로 해야겠다.

(다만, case 에서 multiple row는 리턴하지 못하도록 되어있으니 주의)

[링크 : https://stackoverflow.com/questions/15175187/postgresql-unnest-with-empty-array]

[링크 : https://dba.stackexchange.com/questions/119201/how-does-unnest-treat-null-values-and-empty-arrays]

 

+

SQL 특성(?)인지 당연하게도 해당 내용이 있는지 확인하고

배열이 있으면 해당 배열을 unnest 하고

배열이 없으면 null 배열을 넣어서 처리하도록 하면 해결은 된다..

(null 배열이 아니라 해당 필드가 null 이기 때문에 발생한 경우는 해결 됨)

WITH tbl(id, arr) AS (
   VALUES
     (1, '{a}'::text[])
   , (2, '{NULL}'::text[])
   , (3, '{}'::text[])
   , (4, '{x,y,z}'::text[])
   )
SELECT id, elem
FROM   tbl t
     , unnest (
          CASE WHEN array_length(t.arr, 1) >= 1
               THEN t.arr
               ELSE '{null}'::text[] END
       ) elem;

[링크 : https://dba.stackexchange.com/questions/119201/how-does-unnest-treat-null-values-and-empty-arrays]

Posted by 구차니