Archive

Author Archive

[PostgreSQL] string aggregation

February 8, 2012 Leave a comment

복수의 행 자료를 하나의 문자열로 변환하여 파일로 내보내는 경우 아래와 같은 방식으로 한다.

copy
(
select appno, string_agg(appno_sim, ',')
from sim_pat_54
group by appno
) to 'D:\test\string_agg_54.txt';

Advertisements

[PostgreSQL] Unicode normalization

February 2, 2012 Leave a comment

postgresql-plpython 설치

# yum install postgresql-plpython

쿼리 실행

CREATE LANGUAGE plpythonu;

CREATE OR REPLACE FUNCTION simplify (str text)
RETURNS text
AS $$
import unicodedata

s = unicodedata.normalize('NFKD', str.decode('UTF-8'))
s = ''.join(c for c in s if unicodedata.combining(c) == 0)
return s.encode('UTF-8')
$$ LANGUAGE plpythonu;

TEST

① select simplify(‘特許第2556636号(P2556636)’);

② select simplify(‘Français va à Paris, () {} [] µ @ º Ångstrøm
Phiat-im hû-hō sī phiat tī 1-ê ki-chhó· jī-bó bīn-téng ê hû-hō. Siōng
phó·-phiàn ê kong-lêng sī kái-piàn ki-chhó· jī-bó ê hoat-im.’);

출력결과

① “特許第2556636号(P2556636)”

② “Francais va a Paris, () {} [] μ @ o Angstrøm
Phiat-im hu-ho si phiat ti 1-e ki-chho· ji-bo bin-teng e hu-ho. Siong
pho·-phian e kong-leng si kai-pian ki-chho· ji-bo e hoat-im.”

[참고]
http://wiki.postgresql.org/wiki/Strip_accents_from_strings
http://wiki.postgresql.org/wiki/Strip_accents_from_strings,_and_output_in_lowercase

< 주의할 점! >

컬럼명을 인자로 전달할 때 where 절이 있으면 문제가 없으나, 그렇지 않은 경우 오류가 발생된다. 원인파악이 필요하다.

문제 없는 경우
select uninorm('문자열');
select uninorm(citn_no_original) from t_citn where citn_no_original is not null;

오류발생되는 경우
select uninorm(citn_no_original) from t_citn;
select uninorm(citn_no_original) from t_citn limit 100;

[PostgreSQL] 연번 필드 생성

January 13, 2012 Leave a comment

0000 – 0100 문자열 생성

with recursive foo
AS
(
SELECT 0 seq
UNION ALL
SELECT seq + 1 FROM foo
WHERE seq + 1 <= 100
)
select lpad(cast (seq as varchar(8)), 4, ‘0’) from foo;

Vim 주요 사용법

January 7, 2012 Leave a comment

visual mode
v
Ctrl+v (컬럼 단위). ※ gvim 에서는 Ctrl+q 이며, 일괄 insert, append 시 대문자 “I”, “A” 사용.

SELinux

January 7, 2012 Leave a comment

기능끄기

# vi /etc/sysconfig/selinux
SELINUX=enforcing –>  SELINUX=disabled
# reboot

wget

January 6, 2012 Leave a comment

wget -x -nH –cut-dirs=1 -N -c -i url.txt

[PostgreSQL] dblink

December 22, 2011 Leave a comment

dblink_connect

dblink_connect (text connstr) returns text
dblink_connect (text conname, text connstr) returns text

(example 1)
SELECT dblink_connect (‘connect_name’, ‘hostaddr=10.3.4.13 port=5432 dbname=db_kr_patent user=postgres password=xxxx’);

(example 2)
CREATE TABLE t_publish AS
SELECT id_kipi, publn_nat_cd_std, dnum_kipi, dnum_jgr, doc_kind_std, doc_publn_date
FROM dblink(‘jpnips_connect’,’select id_kipi, publn_nat_cd_std, dnum_kipi, dnum_jgr, doc_kind_std, doc_publn_date from t_publish’)
AS t1 (  id_kipi character varying(32),
publn_nat_cd_std character varying(8),
dnum_kipi character varying(32),
dnum_jgr character varying(32),
doc_kind_std character varying(8),
doc_publn_date character varying(16)
);