Concatenate column values from multiple rows

Ask a Question related to IBM DB2, Design and Development.

  1. #1

    Default Concatenate column values from multiple rows

    Greetings,

    Would it be possible to construct SQL to concatenate column values
    from multiple rows?

    SELECT ... FROM T1, T2 WHERE T1.key=T2.fkey (group by key?);


    The following is an example:

    Table 1 (key and other columns):
    key
    ---
    A
    B
    C

    Table 2 (fkey, col1 etc.):
    fkey col1
    ---- ----
    A 1
    A 2
    A 3
    B 1
    B 2
    C 4

    The SQL to be constructed should return ALL col1 values concatenated
    for the same key column:

    key con-col1
    --- --------
    A 123
    B 12
    C 4

    Any hints or suggestions would be greatly appreciated.

    Platform is DB2 V8 on W2K + FP2.
    nek Guest

  2. Similar Questions and Discussions

    1. Concatenate fields with no values
      I am running CF 5 with SQL Server 2000. I am looking to concatenate a person name, but not all names have a suffix or middle name. Sample SQL:...
    2. Can you concatenate recordset values in one field?
      Hi I have an Access db which stores, amongst other things, details of small mailing lists, people who have received a particular mailing. Because...
    3. multiple menu/list values inserted into one mysql column
      Simple problem here for the pros I'm sure. I have constructed a "rockshow" database in mysql for local music artists/bands and I made an input form...
    4. How To: handle DataGrid row Click Event that passes rows column values to server-side code behind function
      I've looked through many posted messages, and have tried several things but have not seemed to solve this (what you'd think would be a simple)...
    5. How to concatenate rows of data into one field?
      In this example below, I basically want to concatenate all the names in same department to one field. Example table with data Unique ID Dept...
  3. #2

    Default Re: Concatenate column values from multiple rows

    nek <nekiv90@hotmail.com> wrote:
    > Greetings,
    >
    > Would it be possible to construct SQL to concatenate column values
    > from multiple rows?
    >
    > SELECT ... FROM T1, T2 WHERE T1.key=T2.fkey (group by key?);
    >
    >
    > The following is an example:
    >
    > Table 1 (key and other columns):
    > key
    > ---
    > A
    > B
    > C
    >
    > Table 2 (fkey, col1 etc.):
    > fkey col1
    > ---- ----
    > A 1
    > A 2
    > A 3
    > B 1
    > B 2
    > C 4
    >
    > The SQL to be constructed should return ALL col1 values concatenated
    > for the same key column:
    >
    > key con-col1
    > --- --------
    > A 123
    > B 12
    > C 4
    >
    > Any hints or suggestions would be greatly appreciated.
    You could use recursive SQL like this:

    WITH x(key, val, rnum) AS
    ( SELECT fkey, col1, row_number() over(partition by fkey)
    FROM myTable ),
    y(key, str, cnt, cnt_max) AS
    ( SELECT key, VARCHAR('', 1000), 0, MAX(rnum)
    FROM x
    GROUP BY key
    UNION ALL
    SELECT y.key, y.str || RTRIM(CHAR(x.val)), y.cnt + 1, y.cnt_max
    FROM x, y
    WHERE x.key = y.key AND
    x.rnum = y.cnt + 1 AND
    y.cnt < y.cnt_max )
    SELECT key, str
    FROM y
    WHERE y.cnt = y.cnt_max;

    "myTable" corresponds to the second table you listed above. If you want to
    restrict the result in some way, you only have to modify the common table
    expression named "x".

    --
    Knut Stolze
    Information Integration
    IBM Germany / University of Jena
    Knut Stolze Guest

  4. #3

    Default Re: Concatenate column values from multiple rows

    Thanks Knut! It returned what was expected.

    One problem is its reponse time. Here is the result of running it in
    db2batch:

    Stats:
    (1) MyTable: 80,000 rows of 97 bytes record length
    (2) x: 15,000 rows of 85 bytes record length

    Elapsed Time (s) 2914.922
    Rows fetched: 5752
    Rows printed: 5752

    Arith. mean: 2914.922
    Geom. mean: 2914.922

    Not really optimal considering these tables being two of the six
    tables joined in the query, indexed and clustered on 'key' and 'fkey'
    respectively. The query was run after reorg.

    Best Regards,
    Nek.


    Knut Stolze <stolze@de.ibm.com> wrote in message news:<bk6lba$rl9$1@fsuj29.rz.uni-jena.de>...
    > nek <nekiv90@hotmail.com> wrote:
    >
    > > Greetings,
    > >
    > > Would it be possible to construct SQL to concatenate column values
    > > from multiple rows?
    > >
    > > SELECT ... FROM T1, T2 WHERE T1.key=T2.fkey (group by key?);
    > >
    > >
    > > The following is an example:
    > >
    > > Table 1 (key and other columns):
    > > key
    > > ---
    > > A
    > > B
    > > C
    > >
    > > Table 2 (fkey, col1 etc.):
    > > fkey col1
    > > ---- ----
    > > A 1
    > > A 2
    > > A 3
    > > B 1
    > > B 2
    > > C 4
    > >
    > > The SQL to be constructed should return ALL col1 values concatenated
    > > for the same key column:
    > >
    > > key con-col1
    > > --- --------
    > > A 123
    > > B 12
    > > C 4
    > >
    > > Any hints or suggestions would be greatly appreciated.
    >
    > You could use recursive SQL like this:
    >
    > WITH x(key, val, rnum) AS
    > ( SELECT fkey, col1, row_number() over(partition by fkey)
    > FROM myTable ),
    > y(key, str, cnt, cnt_max) AS
    > ( SELECT key, VARCHAR('', 1000), 0, MAX(rnum)
    > FROM x
    > GROUP BY key
    > UNION ALL
    > SELECT y.key, y.str || RTRIM(CHAR(x.val)), y.cnt + 1, y.cnt_max
    > FROM x, y
    > WHERE x.key = y.key AND
    > x.rnum = y.cnt + 1 AND
    > y.cnt < y.cnt_max )
    > SELECT key, str
    > FROM y
    > WHERE y.cnt = y.cnt_max;
    >
    > "myTable" corresponds to the second table you listed above. If you want to
    > restrict the result in some way, you only have to modify the common table
    > expression named "x".
    nek Guest

  5. #4

    Default Re: Concatenate column values from multiple rows

    nek <nekiv90@hotmail.com> wrote:
    > One problem is its reponse time. Here is the result of running it in
    > db2batch:
    >
    > Stats:
    > (1) MyTable: 80,000 rows of 97 bytes record length
    > (2) x: 15,000 rows of 85 bytes record length
    >
    > Elapsed Time (s) 2914.922
    > Rows fetched: 5752
    > Rows printed: 5752
    >
    > Arith. mean: 2914.922
    > Geom. mean: 2914.922
    >
    > Not really optimal considering these tables being two of the six
    > tables joined in the query, indexed and clustered on 'key' and 'fkey'
    > respectively. The query was run after reorg.
    Maybe using aggregate functions (where the aggregation is actually the
    concatenation) would be better performing:
    [url]http://www7b.boulder.ibm.com/dmdd/library/techarticle/0309stolze/0309stolze.html[/url]

    --
    Knut Stolze
    Information Integration
    IBM Germany / University of Jena
    Knut Stolze Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139