`
ktnd
  • 浏览: 17299 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Another Way to Solve Last Problem.

阅读更多

http://www.oracle.com/technology/oramag/code/tips2004/050304.html


1. create a function to concatenate strings.

CREATE OR REPLACE FUNCTION rowtocol( 
       p_slct IN VARCHAR2,
       p_dlmtr IN VARCHAR2 DEFAULT ',' 
) RETURN VARCHAR2 

  AUTHID CURRENT_USER AS

TYPE c_refcur IS REF CURSOR;

     lc_str VARCHAR2(4000);
     lc_colval VARCHAR2(4000);
     c_dummy c_refcur;

     BEGIN

     OPEN c_dummy FOR p_slct;
     LOOP
       FETCH c_dummy INTO lc_colval;
       EXIT WHEN c_dummy%NOTFOUND;
       lc_str := lc_str || p_dlmtr || lc_colval;
     END LOOP;

     CLOSE c_dummy;
     RETURN SUBSTR(lc_str,2);

     /* 
     EXCEPTION 
     WHEN OTHERS THEN
       lc_str := SQLERRM;
  
       IF c_dummy%ISOPEN THEN
          CLOSE c_dummy;
       END IF;
  
       RETURN lc_str;
     */
    END;


 Usage :

select distinct t.author, 
          rowtocol('select book from table_name where author=''' || t.author|| '''')
from table_name t
;


 or


select t.author, rowtocol('select book from table_name where author=''' || t.author|| '''', '#')
from table_name t
group by t.author
;

 


cons: less natural than user-defined aggregate function 

pros: more flexible, aggregate function could take only one parameter

 

分享到:
评论

相关推荐

    英语四级作文写作框架.pdf

    4. 强调只有通过这种方式才能成功解决问题:"Only in this way can we successfully solve the problem."。 开头万能公式: 1. 使用名言或自创名言:"A proberb says, ‘You are only young once.’ It goes ...

    六级作文作文

    8) By comparison with ..., it decreased/increased/fell from...to... 9) ...rise rapidly(slowly)  10) ...remain level... 11) ...reach ...  12)There is a slight/slow/steady/rapid rise/increase demand./...

    经典英语四级作文框架.pdf

    第四句是结论,例如“Only in this way can we successfully solve the problem.” 三、现象作文 现象作文是英语四级考试中的一种常见的写作形式。在这种框架中,第一段只写两句。第一句是对现象的描述,例如“In ...

    a project model for the FreeBSD Project.7z

    [1] This paper will provide such a project model and is donated to the FreeBSD Documentation project where it can evolve together with the project so that it can at any point in time reflect the way ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    You can declare functions in a way that allows the compiler to expand them inline rather than calling them through the usual function call mechanism. Pros: Inlining a function can generate more ...

    外文翻译 stus MVC

    JSP tags solved only part of our problem. We still have issues with validation, flow control, and updating the state of the application. This is where MVC comes to the rescue. MVC helps resolve some ...

    ChromeCacheView

    Made a small fix that hopefully will solve a crash problem that some users experienced. Version 1.21: Fixed bug: ChromeCacheView failed to copy cache files because the filenames contained invalid ...

    Object- Oriented Programming with Ansi-C

    way if you do not want to (or know how to), and it turns out that you can do just as well with plain ANSI-C. Only object-orientation permits code reuse between projects — although the idea of ...

    ICS delphixe10源码版

    Here is the direct URL at Amazon UK (nearest to my home, please don't use another): http://www.amazon.co.uk/exec/obidos/gc-email-order1/ref=g_gc_email/202-6198323-6681414 For more generous amount, ...

    ImpREC 1.7c

    - Fixed ImageBase problem with DLL's when "Use PE Header from Disk" is checked (jstorme) - Added an "ImpREC Classic" looking version v1.7a FINAL (PUBLIC VERSION) ---------------------------- - ...

    occam一维反演

    C OF THE FORWARD PROBLEM, AND TO MAKE MAXIMUM USE OF DYNAMIC MEMORY ALLOCATION, C AS THE ARRAY SIZES FOR THE 2D PROBLEM ARE NOW GETTING TOO LARGE FOR COMFORT. C INCLUDE FILES ARE NOW USED TO ...

Global site tag (gtag.js) - Google Analytics