想象两张桌子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>