• <legend id='zVGi7'><style id='zVGi7'><dir id='zVGi7'><q id='zVGi7'></q></dir></style></legend>
  • <tfoot id='zVGi7'></tfoot>

    <small id='zVGi7'></small><noframes id='zVGi7'>

        <bdo id='zVGi7'></bdo><ul id='zVGi7'></ul>
    1. <i id='zVGi7'><tr id='zVGi7'><dt id='zVGi7'><q id='zVGi7'><span id='zVGi7'><b id='zVGi7'><form id='zVGi7'><ins id='zVGi7'></ins><ul id='zVGi7'></ul><sub id='zVGi7'></sub></form><legend id='zVGi7'></legend><bdo id='zVGi7'><pre id='zVGi7'><center id='zVGi7'></center></pre></bdo></b><th id='zVGi7'></th></span></q></dt></tr></i><div id='zVGi7'><tfoot id='zVGi7'></tfoot><dl id='zVGi7'><fieldset id='zVGi7'></fieldset></dl></div>

      1. 如何在 Oracle 中将多行组合成逗号分隔的列表?

        时间:2023-07-18

        <small id='mQqc2'></small><noframes id='mQqc2'>

        <tfoot id='mQqc2'></tfoot>
            <tbody id='mQqc2'></tbody>
          <i id='mQqc2'><tr id='mQqc2'><dt id='mQqc2'><q id='mQqc2'><span id='mQqc2'><b id='mQqc2'><form id='mQqc2'><ins id='mQqc2'></ins><ul id='mQqc2'></ul><sub id='mQqc2'></sub></form><legend id='mQqc2'></legend><bdo id='mQqc2'><pre id='mQqc2'><center id='mQqc2'></center></pre></bdo></b><th id='mQqc2'></th></span></q></dt></tr></i><div id='mQqc2'><tfoot id='mQqc2'></tfoot><dl id='mQqc2'><fieldset id='mQqc2'></fieldset></dl></div>

            <legend id='mQqc2'><style id='mQqc2'><dir id='mQqc2'><q id='mQqc2'></q></dir></style></legend>
                  <bdo id='mQqc2'></bdo><ul id='mQqc2'></ul>
                • 本文介绍了如何在 Oracle 中将多行组合成逗号分隔的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的查询:

                  select * from countries
                  

                  结果如下:

                  country_name
                  ------------
                  Albania
                  Andorra
                  Antigua
                  .....
                  

                  我想在一行中返回结果,所以像这样:

                  I would like to return the results in one row, so like this:

                  Albania, Andorra, Antigua, ...
                  

                  当然,我可以编写一个 PL/SQL 函数来完成这项工作(我已经在 Oracle 10g 中这样做了),但是有没有更好的,最好是非 Oracle 特定的解决方案(或者可能是一个内置函数)为了这个任务?

                  Of course, I can write a PL/SQL function to do the job (I already did in Oracle 10g), but is there a nicer, preferably non-Oracle-specific solution (or may be a built-in function) for this task?

                  我通常会使用它来避免子查询中的多行,因此如果一个人拥有多个公民身份,我不希望她/他在列表中重复.

                  I would generally use it to avoid multiple rows in a sub-query, so if a person has more then one citizenship, I do not want her/him to be a duplicate in the list.

                  我的问题基于 SQL server 2005.

                  更新:我的函数如下所示:

                  CREATE OR REPLACE FUNCTION APPEND_FIELD (sqlstr in varchar2, sep in varchar2 ) return varchar2 is
                  ret varchar2(4000) := '';
                  TYPE cur_typ IS REF CURSOR;
                  rec cur_typ;
                  field varchar2(4000);
                  begin
                       OPEN rec FOR sqlstr;
                       LOOP
                           FETCH rec INTO field;
                           EXIT WHEN rec%NOTFOUND;
                           ret := ret || field || sep;
                       END LOOP;
                       if length(ret) = 0 then
                            RETURN '';
                       else
                            RETURN substr(ret,1,length(ret)-length(sep));
                       end if;
                  end;
                  

                  推荐答案

                  这里有一个简单的方法,无需拖延或创建函数.

                  Here is a simple way without stragg or creating a function.

                  create table countries ( country_name varchar2 (100));
                  
                  insert into countries values ('Albania');
                  
                  insert into countries values ('Andorra');
                  
                  insert into countries values ('Antigua');
                  
                  
                  SELECT SUBSTR (SYS_CONNECT_BY_PATH (country_name , ','), 2) csv
                        FROM (SELECT country_name , ROW_NUMBER () OVER (ORDER BY country_name ) rn,
                                     COUNT (*) OVER () cnt
                                FROM countries)
                       WHERE rn = cnt
                  START WITH rn = 1
                  CONNECT BY rn = PRIOR rn + 1;
                  
                  CSV                                                                             
                  --------------------------
                  Albania,Andorra,Antigua                                                         
                  
                  1 row selected.
                  

                  正如其他人提到的,如果您使用的是 11g R2 或更高版本,您现在可以使用更简单的 listagg.

                  As others have mentioned, if you are on 11g R2 or greater, you can now use listagg which is much simpler.

                  select listagg(country_name,', ') within group(order by country_name) csv
                    from countries;
                  
                  CSV                                                                             
                  --------------------------
                  Albania, Andorra, Antigua
                  
                  1 row selected.
                  

                  这篇关于如何在 Oracle 中将多行组合成逗号分隔的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:内连接 vs Where 下一篇:我什么时候应该使用 CROSS APPLY 而不是 INNER JOIN?

                  相关文章

                  <small id='MFM7L'></small><noframes id='MFM7L'>

                  <legend id='MFM7L'><style id='MFM7L'><dir id='MFM7L'><q id='MFM7L'></q></dir></style></legend>
                • <i id='MFM7L'><tr id='MFM7L'><dt id='MFM7L'><q id='MFM7L'><span id='MFM7L'><b id='MFM7L'><form id='MFM7L'><ins id='MFM7L'></ins><ul id='MFM7L'></ul><sub id='MFM7L'></sub></form><legend id='MFM7L'></legend><bdo id='MFM7L'><pre id='MFM7L'><center id='MFM7L'></center></pre></bdo></b><th id='MFM7L'></th></span></q></dt></tr></i><div id='MFM7L'><tfoot id='MFM7L'></tfoot><dl id='MFM7L'><fieldset id='MFM7L'></fieldset></dl></div>
                  <tfoot id='MFM7L'></tfoot>

                  • <bdo id='MFM7L'></bdo><ul id='MFM7L'></ul>