Re: how to create aggregate xml document in 8.3?

From: "Matt Magoffin" <postgresql(dot)org(at)msqr(dot)us>
To: pgsql-general(at)postgresql(dot)org
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: how to create aggregate xml document in 8.3?
Date: 2007-12-11 23:02:01
Message-ID: 50163.192.168.1.108.1197414121.squirrel@msqr.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> "Matt Magoffin" <postgresql(dot)org(at)msqr(dot)us> writes:
>> Hello, I'm trying to write a query to return an XML document like
>> <root foo="bar">
>> <range range="x" count="123">
>> <range range="y" count="345">
>> ...
>> </root>
>
> Something like this:
>
> regression=# select xmlelement(name root, xmlagg(x)) from
> regression-# (select xmlelement(name range, xmlattributes(string4,
> count(*) as count)) as x from tenk1 group by string4) ss;
> xmlelement
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
> <root><range string4="OOOOxx" count="2500"/><range string4="AAAAxx"
> count="2500"/><range string4="HHHHxx" count="2500"/><range
> string4="VVVVxx" count="2500"/></root>
> (1 row)
>
> You need a subquery because your setup requires two levels of
> aggregation: one to make the grouped counts, and then another one
> for the xmlagg() (which is basically just text concatenation).

Thanks very much, that helps. Now I'm wondering if it's also possible to
then fill in another nested element level in the XML output, from the rows
that are aggregated into the <range> count. So from your example,
something like

<root>
<range string4="0000xx" count="2500">
<string4 foo="bar1"/>
<string4 foo="bar2"/>
</range>
<range string4="AAAAxx" count="2500">
<string4 foo="bar3"/>
....
</range>
....
</root>

Any thoughts on this?

-- m@

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dann Corbit 2007-12-11 23:10:35 Re: Killing a session in windows
Previous Message Tom Lane 2007-12-11 22:50:46 Re: Killing a session in windows