Oracle 相当于 SQL Server STUFF 函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2108194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:50:40  来源:igfitidea点击:

Oracle equivalent to SQL Server STUFF function?

oracleselect

提问by Jimmy

Does Oracle have its own implementation of SQL Server stufffunction?

Oracle 是否有自己的 SQL Serverstuff功能实现?

Stuff allows you to receive one value from a multi row select. Consider my situation below

Stuff 允许您从多行选择中接收一个值。考虑我下面的情况

 ID   HOUSE_REF   PERSON
 1      A         Dave
 2      A         John
 3      B         Bob

I would like to write a select statement, but I want the PERSONnames to be in a single row.

我想写一个选择语句,但我希望PERSON名称在一行中。

For example, when I select from this table, I want to achieve the following

例如,当我从这个表中选择时,我想实现以下目标

HOUSE_REF   PERSONS
A           Dave, John
B           Bob

I haven't been able to find a simple solution so far, it may be a case of writing my own function to use, but I'm not entirely sure of how to approach this, any ideas?

到目前为止我还没有找到一个简单的解决方案,可能是编写我自己的函数来使用,但我不完全确定如何解决这个问题,有什么想法吗?

The main business use of this, will be to have a select statement that shows each house, and against that house to have one column which lists everyone that lives in that house. The house ref in this select must be unique, hence needing to concatenate the persons

这个的主要业务用途是有一个选择语句来显示每个房子,并针对那个房子有一个列列出住在那个房子里的每个人。此选择中的房屋参考必须是唯一的,因此需要连接人

Thanks

谢谢

采纳答案by retronym

You can write a custom aggregate function to do this. This string you generate is limited to 4k characters.

您可以编写自定义聚合函数来执行此操作。您生成的此字符串限制为 4k 个字符。

http://www.sqlsnippets.com/en/topic-11591.html

http://www.sqlsnippets.com/en/topic-11591.html

There is an undocumented, unsupported function WMSYS.WM_CONCATto do the same thing.

有一个未记录的、不受支持的函数WMSYS.WM_CONCAT可以做同样的事情。

http://www.psoug.org/reference/undocumented.html

http://www.psoug.org/reference/undocumented.html

回答by Tony Andrews

Oracle 11.2 includes a new function LISTAGGto do this.

Oracle 11.2 包含一个新函数LISTAGG来执行此操作。

Prior to that you could use Tom Kyte's STRAGG function.

在此之前,您可以使用Tom Kyte 的 STRAGG 功能

回答by dpbradley

The "no add-ons/no undocumented functions" Oracle solution (prior to 11.2 as Tony mentions) is:

“没有附加组件/没有未记录的功能”Oracle 解决方案(在 Tony 提到的 11.2 之前)是:

select c1, ltrim(sys_connect_by_path(c2,','),',') persons
 from
  (
   select c1, c2, 
    row_number() over (partition by c1 order by c2 ) rn
     from
      (
       select house_ref c1, person c2 
        from housetable 
      )
   )
  where connect_by_isleaf=1
  connect by prior rn+1 =rn and prior c1 = c1
  start with rn=1
;

回答by nitin bhurbhure

Execute below three functions.

执行以下三个功能。

Function 1

功能一

create or replace type stragg_type as object ( string varchar2(4000),

static function ODCIAggregateInitialize ( sctx in out stragg_type ) return number ,

member function ODCIAggregateIterate ( self in out stragg_type , value in varchar2 ) return number ,

member function ODCIAggregateTerminate ( self in stragg_type, returnvalue out varchar2, flags in number ) return number ,

member function ODCIAggregateMerge ( self in out stragg_type, ctx2 in stragg_type ) return number );

创建或替换类型 stragg_type 作为对象(字符串 varchar2(4000),

静态函数 ODCIAggregateInitialize(sctx in out stragg_type)返回数字,

成员函数 ODCIAggregateIterate(self in out stragg_type,varchar2 中的值)返回数字,

成员函数 ODCIAggregateTerminate(stragg_type 中的 self,varchar2 中的返回值,number 中的标志)返回 number ,

成员函数 ODCIAggregateMerge ( self in out stragg_type, ctx2 in stragg_type ) return number );

/

/

function 2

功能二

create or replace type body stragg_type is

static function ODCIAggregateInitialize ( sctx in out stragg_type ) return number is begin

sctx := stragg_type( null ) ;

return ODCIConst.Success ;

end;

member function ODCIAggregateIterate ( self in out stragg_type , value in varchar2 ) return number is begin

self.string := self.string || ',' || value ;

return ODCIConst.Success;

end;

member function ODCIAggregateTerminate ( self
in stragg_type , returnvalue out varchar2 , flags in number ) return number is begin

returnValue := ltrim( self.string, ',' );

return ODCIConst.Success;

end;

member function ODCIAggregateMerge
( self in out stragg_type , ctx2 in stragg_type ) return number is begin

self.string := self.string || ctx2.string;

return ODCIConst.Success;

end;

end; /

创建或替换类型主体 stragg_type 是

静态函数 ODCIAggregateInitialize ( sctx in out stragg_type ) 返回数字是开始

sctx := stragg_type( null ) ;

return ODCIConst.Success ;

结尾;

成员函数 ODCIAggregateIterate ( self in out stragg_type , value in varchar2 ) return number is begin

self.string := self.string || ',' || value ;

return ODCIConst.Success;

结尾;

成员函数 ODCIAggregateTerminate(stragg_type
中的 self,varchar2 中的返回值,number 中的标志)返回数字是 begin

returnValue := ltrim( self.string, ',' );

return ODCIConst.Success;

结尾;

成员函数 ODCIAggregateMerge
( self in out stragg_type , ctx2 in stragg_type ) 返回编号为开始

self.string := self.string || ctx2.string;

return ODCIConst.Success;

结尾;

结尾; /

function 3

功能 3

create or replace function stragg ( input varchar2 ) return varchar2
deterministic parallel_enable
aggregate using stragg_type ; /

创建或替换函数 stragg(输入 varchar2) 使用 stragg_type返回 varchar2
确定性 parallel_enable
聚合;/



after executing three functions now u can use stragg as follows

现在执行三个函数后,你可以使用 stragg 如下

Example Table :emp name | salary a
| 100 a | 200 a | 300 b | 400 b | 500 c | 600 c | 700 d
| 800

select name , STRAGG( salary) as string from emp group by name;

示例表:emp 名称 | 工资
| 100个| 200个| 300 b | 400 b | 500 c | 600 c | 700 天
| 800

按名称从 emp 组中选择 name , STRAGG(salary) 作为字符串;

output:

输出:

a | 100,200,300

b | 400,500

c | 600,700

d | 800

一个| 100,200,300

乙 | 400,500

c | 600,700

d | 800