티스토리 뷰

반응형

인포믹스에서는 listagg나 group_concat 같은 함수가 제공되지 않습니다.

인포믹스 15버전부터는 group_concat 함수가 자체 기능으로 지원됩니다!


프로시저나 프로그램을 사용해야합니다.

아래는 stack overflow의 글을 참고하여 인포믹스 11.5버전에서 테스트한 예시입니다.
AGGREGATE에 대한 자세한 설명은 아래의 IBM 문서를 참고해주세요.

$ dbaccess stores_demo -

> CREATE FUNCTION gc_init(dummy VARCHAR(255)) RETURNING LVARCHAR;
    RETURN '';
END FUNCTION;

Routine created.

> CREATE FUNCTION gc_iter(result LVARCHAR, value VARCHAR(255))
    RETURNING LVARCHAR;
    IF result = '' THEN
        RETURN TRIM(value);
    ELSE
        RETURN result || ',' || TRIM(value);
    END IF;
END FUNCTION;

Routine created.

> CREATE FUNCTION gc_comb(partial1 LVARCHAR, partial2 LVARCHAR)
    RETURNING LVARCHAR;
    IF partial1 IS NULL OR partial1 = '' THEN
        RETURN partial2;
    ELIF partial2 IS NULL OR partial2 = '' THEN
        RETURN partial1;
    ELSE
        RETURN partial1 || ',' || partial2;
    END IF;
END FUNCTION;

Routine created.

> CREATE FUNCTION gc_fini(final LVARCHAR) RETURNING LVARCHAR;
    RETURN final;
END FUNCTION;

Routine created.

> CREATE AGGREGATE group_concat
    WITH (INIT = gc_init, ITER = gc_iter,
          COMBINE = gc_comb, FINAL = gc_fini);

Aggregate created.

위와 같이 FUNCTION과 AGGREGATE를 결합하면 group_concat과 유사한 기능을 구현할 수 있네요.

> create table test (name varchar(20),nickname varchar(20));

Table created.
 
> insert into test values ('홍길동','구름');
 
1 row(s) inserted.
 
> insert into test values ('심청이','달');
 
1 row(s) inserted.
 
> insert into test values ('변사또','해');
 
1 row(s) inserted.
 
> insert into test values ('홍길동','달');
 
1 row(s) inserted.
 
> insert into test values ('심청이','별');
 
1 row(s) inserted.
 
> insert into test values ('변사또','물');
 
1 row(s) inserted.
 
 
> select name, group_concat(nickname) from test group by name;
 
 
 
name          홍길동
group_concat  구름,달
 
name          변사또
group_concat  해,물
 
name          심청이
group_concat  달,별
 
3 row(s) retrieved.

 

 

Show a one to many relationship as 2 columns - 1 unique row (ID & comma separated list)

I need something similar to these 2 SO questions, but using Informix SQL syntax. Concatenate several fields into one with SQL SQL Help: Select statement Concatenate a One to Many relationship M...

stackoverflow.com

CREATE AGGREGATE statement - IBM Documentation

 

CREATE AGGREGATE statement

Use the CREATE AGGREGATE statement to create a new aggregate function and register it in the sysaggregates system catalog table. User-defined aggregates (UDA) extend the functionality of the database server by performing aggregate calculations that the use

www.ibm.com

 

반응형
댓글