1개 테이블에 다른 테이블의 값이 여러개 있어서

어떻게 한번에  join이 가능한지 보는데.. 별거 없네

JOIN ON에 여러개 붙이면 된다.

 

[링크 : https://stackoverflow.com/questions/26916741/how-to-join-two-tables-by-multiple-columns-in-sql/26916803]

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

string_agg() + COALESCE() + DISTINCT  (0) 2019.09.23
join where와 on  (0) 2019.09.18
postgresql update  (0) 2019.09.18
postgresql 정수형에 "NULL"이 null로 인식을 못할때  (0) 2019.09.18
query tweak join vs large table  (0) 2019.09.17
Posted by 구차니

자기 자신의 필드 두개의 값을 . 으로 서로 간에 이어주려고 하는데(예 a,b -> a.b)

어떻게 하나 고민을 하는데.. 어라? where 조건이 optional 이었네?

[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table [ * ] [ [ AS ] alias ]
    SET { column = { expression | DEFAULT } |
          ( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
    [ FROM from_list ]
    [ WHERE condition | WHERE CURRENT OF cursor_name ]
    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

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

 

그래서~!

UPDATE table SET column = concat(a,'.',b) WHERE a is not null;

으로 하니 원하는 대로 값이 있는 녀석들에 대해서만 a.b 식으로 붙여서 새로운 컬럼에 넣어줄 수 있다.

[링크 : https://blog.naver.com/troopa102/120160231056]

 

생각해보니 게시판 등에서 WHERE를 많이 쓴 이유가

하나의 내용을 수정하기 위함이라 Primary Key를 이용해야 하니

WHERE pk = idx 식으로 고정을 했던거고

 

나의 경우에는 테이블 전체에 대해서 어떠한 조건에 의해 수정하는 것이기에

수정할 조건(나의 경우에는 not null)로 해주면 자기 자신의 값을 모두 업데이트 할 수 있네?

 

오.. SQL 신기하고 재미있네 +_+

병렬처리 하는 느낌 좋음 ㅋㅋ

 

 

+

2019.09.30

regexp_matches()는 여러줄을 뱉는 애라 update에서 못쓰니

subtring()을 써서 정규표현식으로 자르면 된다고 한다.

[링크 : https://stackoverflow.com/questions/936513/postgresql-query-to-update-fields-using-a-regular-expression]

Posted by 구차니

오류:  정수 자료형 대한 잘못된 입력 구문: "NULL"

 

\COPY FROM 명령을 통해서 csv import 하려는데 에러가 나서

이런저런 옵션을 조합해 보다내린 결론은.. CSV 파일을 잘못 만들었다!!! 으아아아아앙!!!

 

그래서.. "NULL" 으로 표기된 문자를 sed를 통해서 ,, 으로 빈값으로 변환시켜주면

psql 에서 정상적으로 NULL로 받아올 수 있다.

sed 's/\"NULL\"//g' inputfile > outputfile

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

복수 컬럼에 대한 inner join  (0) 2019.09.18
postgresql update  (0) 2019.09.18
query tweak join vs large table  (0) 2019.09.17
postgresql csv import null as null  (0) 2019.09.11
cross join  (0) 2019.09.08
Posted by 구차니

join은 확실히 무거운 녀석이라. 큰 테이블과 큰 테이블을 join하면 메모리부터

어마어마하게 부하가 걸린다.

 

일단은.. large table이 유리한 것으로 보이는데

정규화로는 어떻게 할 수 있는 건 아닌것 같으니 이렇게 하는 걸지도?

 

[링크 : https://medium.com/.../blazingly-fast-querying-on-huge-tables-by-avoiding-joins-5be0fca2f523]

Posted by 구차니

null로 써있는 것을 DB에 끌어갈때는 NULL로 인식시킨다고 하는데 해보긴 해야겠네..

 

 copy r from '/home/y.csv' delimiter ',' csv NULL AS 'null';

[링크 : https://stackoverflow.com/questions/19034674/copy-null-values-present-in-csv-file-to-postgres]

Posted by 구차니

2.14.0 쓰고 있었는데

2.15.x 부터 3-way diff를 지원한다고 한다.

이제.. kdiff3 안써도 되겠다 +_+!

[링크 : https://winmerge.org/docs/releasenotes.php]

Posted by 구차니

내가 원하는건 아래의 예인데..

CROSS JOIN

 

I get: 
Col1 | Col2 | Col3 | Col4 | Col5  
--------------------------------- 
NULL | NULL | NULL |  2   |  0 
 7   |  3   |  4   | NULL | NULL 


I want to get 

Col1 | Col2 | Col3 | Col4 | Col5  
--------------------------------- 
  7  |  3   |   4  |  2   |  0

 

막상 쿼리를 보니 그냥 두개 합쳐서 골라서 빼내는 것 같네..

자동으로 null은 없애고 합치는건 없으려나?

SELECT B.Col1 
    ,B.col2 
    ,B.col3 
    ,A.col4 
    ,A.col5 
FROM ( 
    SELECT NULL AS Col1 
        ,NULL AS Col2 
        ,NULL AS Col3 
        ,2 AS Col4 
        ,0 AS Col5 
    ) A 
CROSS JOIN ( 
    SELECT 7 AS Col1 
        ,3 AS Col2 
        ,4 AS Col3 
        ,NULL AS Col4 
        ,NULL AS Col5 
    ) B

[링크 : https://stackoverflow.com/questions/36078353/sql-server-union-with-multiple-columns/36078940]

 

+

null을 없애고 합치는건 left join이 오히려 유용한건가?

[링크 : https://stackoverflow.com/questions/11461867/sql-cross-join-with-null-values]

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

query tweak join vs large table  (0) 2019.09.17
postgresql csv import null as null  (0) 2019.09.11
sql 여러행을 하나로 합치기 concat  (0) 2019.09.08
값별 갯수 통계  (0) 2019.09.07
postgresql.conf 설정사항들  (0) 2019.09.06
Posted by 구차니

oracle에서는 WM_CONCAT

mysql에서는 GROUP_CONCAT

postgresql 에서는 ARRAY_TO_STRING(ARRAY_AGG(column_name),',')  으로 하면 된다고 한다.

 

[링크 : https://dmsrbdi123.tistory.com/11] postgresql

[링크 : https://gent.tistory.com/15] oracle

[링크 : https://extbrain.tistory.com/48] mysql

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

postgresql csv import null as null  (0) 2019.09.11
cross join  (0) 2019.09.08
값별 갯수 통계  (0) 2019.09.07
postgresql.conf 설정사항들  (0) 2019.09.06
postgresql copy from csv , iso8859-1  (0) 2019.09.05
Posted by 구차니

distinct count만 생각했는데 group by를 써야하는 가였나..

 

위에는 distinct의 영향을 받아 order by column_name이 적용되고

아래는 order by가 적용되지 않는 순서로 나온다.(결과는 동일)

select distinct(column_name), count(*) from table group by(column_name)

select column_name, count(*) from table group by(column_name)

[링크 :https://stackoverflow.com/questions/7053902/sql-how-to-get-the-count-of-each-distinct-value-in-a-column]

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

cross join  (0) 2019.09.08
sql 여러행을 하나로 합치기 concat  (0) 2019.09.08
postgresql.conf 설정사항들  (0) 2019.09.06
postgresql copy from csv , iso8859-1  (0) 2019.09.05
postgresql copy from csv, date null  (0) 2019.09.05
Posted by 구차니

shared_buffers 기본 32M 이지만, 대략 서버 메모리 기준으로 1/4 ~ 1/2 정도로 할당

[링크 : http://hochul.net/blog/postgresql-configuration-tuning-basic-guide/?ckattempt=1]

 

work_mem 쿼리에 해당되는 정렬이나 임시 저장을 위한 공간 확보의 상한선 각 쿼리당으로 설정값이 반영

[링크 : https://arisu1000.tistory.com/1047]

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

sql 여러행을 하나로 합치기 concat  (0) 2019.09.08
값별 갯수 통계  (0) 2019.09.07
postgresql copy from csv , iso8859-1  (0) 2019.09.05
postgresql copy from csv, date null  (0) 2019.09.05
postgresql unknown pseudo-type  (0) 2019.09.05
Posted by 구차니