0

想象两张桌子sites& buildings,其中一个站点可能有多个建筑物。我有一个查询,它为地理空间数据映射生成一个 xml,并尝试添加属于一个站点的所有建筑物,考虑使用LISTAGG函数。

添加建筑物之前的工作查询

SELECT XMLELEMENT("rss", 
            XMLATTRIBUTES('http://www.georss.org/georss' AS "xmlns:georss"),
                    XMLELEMENT("channel",
                                 XMLAGG(XMLELEMENT("item",
                                                   XMLFOREST(title AS "title",
                                                              description AS "description",
                                                              point AS "georss:point"))))).EXTRACT('/*')
    FROM (SELECT trim(s.sitedesc) AS title, 
          'Employee Count: ' || s.sitefte || '. Bldg Gross SF: ' || s.bldgarea || '. ' || CASE WHEN s.leasestat = '0' THEN 'Owned' WHEN s.leasestat = '1' THEN 'Leased, end date: ' || trim(l.edate) END AS description, 
          trim(s.latitude) || ' ' || trim(s.longitude) AS point
          FROM site s, leases l
          WHERE s.sold <> 1
            AND s.longitude != ' '
            AND s.sitecode = l.sitecode
        )

我可以让 LISTAGG 函数自己工作。

SELECT LISTAGG(trim(b.bldgcode)||' '||trim(b.bldgdesc), ', ') WITHIN GROUP (ORDER BY b.bldgcode) AS Bldgs
FROM buildings b, sites s
WHERE b.sitecode = s.sitecode
GROUP BY s.sitedesc

但是,该GROUP BY子句在 xml“不是按表达式分组”中引发错误(我期待但似乎无法修复)。下面的示例,是否可以在 XMLELEMENT 中使用 LISTAGG ?

SELECT XMLELEMENT("rss", 
            XMLATTRIBUTES('http://www.georss.org/georss' AS "xmlns:georss"),
                    XMLELEMENT("channel",
                                 XMLAGG(XMLELEMENT("item",
                                                   XMLFOREST(title AS "title",
                                                              description AS "description",
                                                              point AS "georss:point"))))).EXTRACT('/*')
    FROM (SELECT trim(s.sitedesc) AS title, 
          'Employee Count: ' || s.sitefte || '. Bldg Gross SF: ' || s.bldgarea || '. ' || CASE WHEN s.leasestat = '0' THEN 'Owned' WHEN s.leasestat = '1' THEN 'Leased, end date: ' || trim(l.edate) END || LISTAGG(trim(b.bldgcode)||' '||trim(b.bldgdesc), ', ') WITHIN GROUP (ORDER BY b.bldgcode) AS description, 
          trim(s.latitude) || ' ' || trim(s.longitude) AS point
          FROM site s, lease l, buildings b
          WHERE s.sold <> 1
            AND s.longitude != ' '
            AND s.sitecode = l.sitecode
            AND b.sitecode = s.sitecode
          GROUP BY s.sitedesc
        )

所需的 XML 样本

<rss xmlns:georss="http://www.georss.org/georss">
<channel>
    <item>
        <title>SITE DESCRIPTION</title>
        <description>Employee Count: ###. Bldg Gross SF: ####. Owned/Leased Building1, building2, etc.</description>
        <georss:point>LAT LON</georss:point>
    </item>
....
</channel>
</rss>
4

1 回答 1

1

更改您的组。

select xmlelement("rss", xmlattributes('http://www.georss.org/georss' as "xmlns:georss"), xmlelement("channel", xmlagg(xmlelement("item", xmlforest(title as "title", description as "description", point as "georss:point")))))
       .extract('/*')
  from (select trim(s.sitedesc) as title,
                'Employee Count: ' || s.sitefte || '. Bldg Gross SF: ' || s.bldgarea || '. ' || case
                  when s.leasestat = '0' then
                   'Owned'
                  when s.leasestat = '1' then
                   'Leased, end date: ' || trim(l.edate)
                end || listagg(trim(b.bldgcode) || ' ' || trim(b.bldgdesc), ', ') within group(order by b.bldgcode) as description,
                trim(s.latitude) || ' ' || trim(s.longitude) as point
           from site s, lease l, buildings b
          where s.sold <> 1
            and s.longitude != ' '
            and s.sitecode = l.sitecode
            and b.sitecode = s.sitecode
          group by s.sitedesc, s.sitefte, s.bldgarea, s.leasestat, l.edate, s.latitude, s.longitude)

这应该可以正常工作。

于 2012-11-27T17:15:49.963 回答