0

我意识到这是一个荒谬的请求,但我想要做的是将多条记录连同一些文字文本一起拉回到一个列中。

所以给定一张这样的桌子

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;

当我尝试查看结果时,我们的前端环境确实接受查询,但出现错误:资源无效。在查看之前,您可能需要重新创建修复查询。

我知道这个错误可能没有帮助,但是有什么想法为什么我的版本不起作用?

谢谢!

4

3 回答 3

3

这很恶心,但你可以这样做:

select replace(blah2,',','')
  from ( select wm_concat(blah) as blah2
           from ( select '<option value="' || sid || '">' || city || '</option>' as blah
                    from my_table
                         )
                 )
于 2012-02-22T23:05:24.227 回答
-2

你玩过 DBMS_XMLGEN 吗?

于 2012-02-22T23:44:27.587 回答
-3

您可以在 Oracle 中创建聚合函数,请参阅文档。

于 2012-02-22T22:56:19.753 回答