Oracle/SQL - 多条记录合并为一条记录[字符串聚合]
我意识到这是一个荒谬的请求,但我想做的是将多个记录连同一些文字一起拉回到单个列中。
因此,给定这样的表,
REGION CITY SID
-------------------
1 Chicago 1234
1 Palatine 567
1 Algonquin 234
1 Wauconda 987
我希望看到带有列的单个记录,其他列(例如区域)都可以,但是像这样的单个列
<option value="1234">Chicago</option><option value="567">Palatine</option><option value="234">Algonquin</option><option value="987">Wauconda</option>
对如何执行此操作有什么想法吗?我正在运行 Oracle 9i,无法在 PL/SQL 中执行此操作
好的,表格式已更改了一点,但想法是相同的
COUNTRY STORECODE STORE_NAME
------------------------------
USA 1234 Chicago
USA 567 Palatine
CAN 987 Toronto
所以我发现此代码正在通过列出的链接
SELECT COUNTRY,
LTRIM(MAX(SYS_CONNECT_BY_PATH(STORECODE,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS COUNTRY_HTML
FROM (SELECT COUNTRY,
STORECODE,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY STORECODE) AS curr,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY STORECODE) -1 AS prev
FROM tablename)
GROUP BY COUNTRY
CONNECT BY prev = PRIOR curr AND COUNTRY = PRIOR COUNTRY
START WITH curr = 1;
当我运行它时,我会看到此输出
COUNTRY COUNTRY_HTML
--------------------
USA 1234,567
CAN 987
我的想法只是从另一个选择中提取内部选择,我在其中将 STORECODE 和 STORE_NAME 以及所需的 html 进行连接,如下所示...
SELECT COUNTRY,
LTRIM(MAX(SYS_CONNECT_BY_PATH(RECORD_HTML,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS COUNTRY_HTML
FROM (SELECT COUNTRY,
RECORD_HTML,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY RECORD_HTML) AS curr,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY RECORD_HTML) -1 AS prev
FROM (SELECT COUNTRY, '<option value="' || STORECODE || '">' || STORE_NAME || '</option>' AS RECORD_HTML FROM tablename))
GROUP BY COUNTRY
CONNECT BY prev = PRIOR curr AND COUNTRY = PRIOR COUNTRY
START WITH curr = 1;
虽然我们的前端环境确实接受查询,但当我尝试查看结果时,我得到了错误:资源无效。在查看之前,您可能需要重新创建或修复查询。
我知道该错误可能没有帮助,但是您知道为什么我的版本无法工作吗?
谢谢!
I realize this is a ridiculous request, but what I'm trying to do is pull multiple records back into a single column along with some literal text.
So given a table like this
REGION CITY SID
-------------------
1 Chicago 1234
1 Palatine 567
1 Algonquin 234
1 Wauconda 987
I would like to see back a single record with a column, other columns like region are fine, but a single column like this
<option value="1234">Chicago</option><option value="567">Palatine</option><option value="234">Algonquin</option><option value="987">Wauconda</option>
Any thoughts on how to do this? I'm running Oracle 9i and cannot do this in PL/SQL
Okay the table format has changed a bit, but the idea is the same
COUNTRY STORECODE STORE_NAME
------------------------------
USA 1234 Chicago
USA 567 Palatine
CAN 987 Toronto
So I found this code going through the links listed
SELECT COUNTRY,
LTRIM(MAX(SYS_CONNECT_BY_PATH(STORECODE,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS COUNTRY_HTML
FROM (SELECT COUNTRY,
STORECODE,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY STORECODE) AS curr,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY STORECODE) -1 AS prev
FROM tablename)
GROUP BY COUNTRY
CONNECT BY prev = PRIOR curr AND COUNTRY = PRIOR COUNTRY
START WITH curr = 1;
And when I run it I see this output
COUNTRY COUNTRY_HTML
--------------------
USA 1234,567
CAN 987
My thought was simply to have the inner select pull from another select where I do my concat of the STORECODE and STORE_NAME along with the html required like this...
SELECT COUNTRY,
LTRIM(MAX(SYS_CONNECT_BY_PATH(RECORD_HTML,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS COUNTRY_HTML
FROM (SELECT COUNTRY,
RECORD_HTML,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY RECORD_HTML) AS curr,
ROW_NUMBER() OVER (PARTITION BY COUNTRY ORDER BY RECORD_HTML) -1 AS prev
FROM (SELECT COUNTRY, '<option value="' || STORECODE || '">' || STORE_NAME || '</option>' AS RECORD_HTML FROM tablename))
GROUP BY COUNTRY
CONNECT BY prev = PRIOR curr AND COUNTRY = PRIOR COUNTRY
START WITH curr = 1;
While our front end environment does accept the query when I try to review the results I get a error: the resource is invalid. You may need to re-create of fix the query before viewing.
I know that error probably isn't helpful, but any ideas why my version isn't working?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很恶心,但你可以这样做:
It's disgusting but you could do something like this:
你玩过 DBMS_XMLGEN 吗?
Have you played around with DBMS_XMLGEN?
您可以在 Oracle 中创建聚合函数,请参阅文档。
You can create an aggregate function in Oracle, see documentations.