Skip to main content

Posts

Showing posts from August, 2018

Teradata XMLAGG

SQLServerやPostgreSQLにあるstring_aggやMySQLのgroup_concatですが残念なことにTeradataでは存在しません。代わりにという訳ではないですが、 xmlagg という集約関数があり、これを使うと同じようなことが実現できます。 まずはデータを準備します。 drop table test_xml_agg ; create table test_xml_agg (  c1 int ,c2 int ,c3 varchar(10) ) ; insert into test_xml_agg values (1,1,'hello'); insert into test_xml_agg values (1,2,'world'); insert into test_xml_agg values (2,1,'this'); insert into test_xml_agg values (2,2,'is'); insert into test_xml_agg values (2,3,'xmlagg'); 上記のデータを1列目で集約し、2列目の順番で一度3列目を横に展開しカンマで結合するというクエリを書いてみます。 select  c1    ,trim(trailing ',' from xmlagg (c3 || ',' order by c2) (varchar(100))) as string_agg from test_xml_agg group by 1 ; Result Set c1 string_agg 1  hello, world 2  this, is, xmlagg string_aggに比べるとごちゃちゃしていますが、まず xmlagg(c3 || ',' order by c2) で2列目の昇順で3列目を結合していくことを表しています。続いて (varchar(100)) で型をsysudtlib.xmlからvarcharにキャストし、最後に末尾のカンマを除いています。 2018/10/19追記 tdstats.udfco...