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

postgres 배열 처리하기  (0) 2019.10.15
sql with ,  (0) 2019.10.10
postgresql rank() over()  (0) 2019.10.04
여러 줄을 하나의 값으로 합치기 - array()  (0) 2019.10.02
postgresql LEFT JOIN = LEFT OUTER JOIN  (0) 2019.10.01
Posted by 구차니

중복되는 놈들이 있을때 값이 가장 큰 한놈만 빼서 쓰기 위해 사용한 함수.

그 외에는 어떤 목적으로 써야 하려나?

 

select (

SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC) FROM empsalary;

) where rank = 1;

[링크 : http://www.postgresqltutorial.com/postgresql-rank-function/]

[링크 : https://www.postgresql.org/docs/9.1/tutorial-window.html]

Posted by 구차니

array 안에서는 select 문을 통해 다른 값들을 받아 하나의 필드로 출력을 해준다.

개꿀! (unnest로 풀면되지!)

 

The result I get is:

 +-----------------------+
 | ?column?              |
 +-----------------------+
 | 15:00:00 Dissertation |
 | 17:00:00 Dinner       |
 | 23:00:00 Sleep        |
 +-----------------------+
Now that I have my rows, I can turn them into an array.  Now, the ARRAY function needs to be invoked via a SELECT.  Thus, using ARRAY means that we’re using a subselect.  The inner SELECT is what we did above.  The outer one is just our call to ARRAY:

SELECT ARRAY(SELECT meeting_at::time || ' ' || description 
FROM Appointments 
WHERE meeting_at::date = '2014-may-23'
ORDER BY meeting_at);
And sure enough, we get a one-row, one-column result:

 +--------------------------------------------------------------+
 | array                                                        |
 +--------------------------------------------------------------+
 | {"15:00:00 Dissertation","17:00:00 Dinner","23:00:00 Sleep"} |
 +--------------------------------------------------------------+

[링크 : https://lerner.co.il/2014/05/23/turning-postgresql-rows-arrays-array/]

Posted by 구차니

헐.. INNER , OUTER 보다 보니

LEFT JOIN은 어느걸까 했는데 OUTER일 줄이야..

 

INNER, OUTER 부터 다시 공부해야겠다. ㅠㅠ

[링크 : http://www.postgresqltutorial.com/postgresql-left-join/]

Posted by 구차니

select 문만 쓰다가 update를 join을 통해 하려니 신기한 느낌?

update 문에 table이 하나 있으니 FROM 으로 다른 테이블을 정해주면 자연스럽게(?) join이 된다.

 

UPDATE tb1

SET col2 = tb2.col22

FROM tb2

WHERE tb1.col1 = tb2.col21 

[링크 : https://xshine.tistory.com/209]

Posted by 구차니

select로 추려낸 결과를 다른 select - where 문에서 쓰기 위한 방법

 

[링크 : https://stackoverflow.com/questions/1136380/sql-where-in-clause-multiple-columns]

 

+

2019.10.04

라는데.. 굳이 이걸 써야 하나 싶긴하네

select * from where (val1_upper, val2_upper) in (select val1,val2 from something)

Posted by 구차니

temporary file leak: File 4 still referenced

 

아.. 먼가 불안한 경고다.. 

일단 급한건 아니니 나중에 봐야지..

 

[링크 : https://dba.stackexchange.com/questions/112079/slow-query-performance-due-to-temporary-file]

Posted by 구차니

regexp_matches()는 여러개가 매칭될수 있어서 array()로 리턴하는데

{} 로 쌓여 있어서 그걸 벗기기 위해서는 unnest()를 하는게 가장 간단한데..

 

select 까진 문제없으나..

udpate 시에는 multiple row가 나올 녀석은 아예 배제가 되니 주의

 

[링크 : https://stackoverflow.com/questions/10593400/remove-braces-from-regular-expression-result]

Posted by 구차니

unnest를 사용하니 ,로 구분된 리스트를 여러개의 열로 나눌수 있었다.

string_to_array()의 반대 개념이라고 하면 되려나?

 

regexp_split_to_table() 도 사용할 수 있으나 regexp의 cost가 비싼 편이라 추천은 안하는 듯

[링크 : https://stackoverflow.com/questions/29419993/split-column-into-multiple-rows-in-postgres]

  [링크 : https://www.postgresql.org/docs/current/functions-string.html#FUNCTIONS-STRING-OTHER]

 

+

예제에 따라 다르지만 select에서 써도 되고 from에서 lateral join으로 구현해도 되고

어느게 cost가 낮을려나?

[링크 : https://www.postgresql.org/docs/9.2/functions-array.html]

[링크 : https://wwwi.tistory.com/350]

 

+

 

Posted by 구차니

 

The operator ~~ is equivalent to LIKE, and ~~* corresponds to ILIKE. There are also !~~ and !~~* operators that represent NOT LIKE and NOT ILIKE, respectively. All of these operators are PostgreSQL-specific.

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

 

SQL> select * from test where x ~ '[0-9]+';  -- "~" 는 "similar to" 의미입니다.

[링크 : https://www.postgresdba.com/bbs/board.php?bo_table=B10&wr_id=44]

Posted by 구차니