想象一下两张表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“not by group by expression”中引发错误(我原以为但似乎无法修复)。下面的示例,是否可以在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>
答案 0 :(得分: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)
这应该可以正常工作。