0

サイトに複数の建物がある場合がある2 つのテーブル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 に答える