使用场景:把某一列值转换为逗号分隔的字符串 例子:比如查询所有的的表空间如下,现在要获得所有的表空间用逗号分隔的字符串(比如rman duplicate的时候skip表空间) SQL> select name from v$tablespace; NAME ------------------------------ SYSTEM SYSAUX CTXD APPS_TS_TX_DATA APPS_TS_TX_IDX OWAPUB APPS_TS_QUEUES ODM OLAP APPS_TS_T
今天在统计报表的时候有这么一个需求,将一列字符串拼接成一行,然后展示到新的列中. 每一项的服务列表如下: 最终想要的结果是 sql如下: select AuxTypeName + ',' from ( SELECT DISTINCT(AuxTypeName) FROM _VIPHall_Link_AuxType LEFT JOIN dbo.[_VIPHall] ON [_VIPHall_Link_AuxType].VIPHallID = [_VIPHall].VIPHallID LEFT JOI
一般我们在数据库的表字段存储字典Id,如果有多个的话一般是用,或分隔符分隔(12,14),列表显示的时候是显示字典名,那如果要在数据库将字典Id转成用户看得懂的字典名,该怎么办呢? 我们这时候可以结合之前说到的 字符串分离(Split函数)和 列记录合并成一行 这两篇文章来完成上述功能. SELECT STUFF(( SELECT ',' + d.Name FROM dbo.fn_SplitStr(feild, ',') AS s JOIN dbo.tb_Dictionary AS d ON d
最近在学习的过程中,发现一个挺有意思的函数,它可实现对列值的拼接.下面我们来看看其具体用法. 用法: 对其作用,官方文档的解释如下: For a specified measure, LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the measure column. 即在每个分组内,LISTAGG根据order by子句对列植进
在项目开发中,有时会碰到将列记录合并为一行的情况,例如根据地区将人员姓名合并,或根据拼音首字母合并城市等,下面就以根据地区将人员姓名合并为例,详细讲一下合并的方法. 首先,先建一个表,并添加一些数据,建表代码如下: If OBJECT_ID(N'Demo') Is Not Null Begin Drop Table Demo EndElse Begin Create Table Demo( Area nvarchar(30),
最近在项目中遇到个问题,需要将表中某列字段合并成字符串输出,如果直接通过代码全部读取出来,再遍历进行拼接显然不是最好的方法,所以想着能否在数据读取的时候直接拼接好返回,网上搜了可通过for xml来实现. 首先,准备好需要的数据,脚本如下: if exists (select * from sysObjects where id=object_id('Student')) drop table Student go create table Student ( Id int, Name ) )
select B.enterprise_code, B.enterprise_name, sum(B.h0_overnum) AS over00, sum(B.h1_overnum) AS over01, sum(B.h2_overnum) AS over02, sum(B.h3_overnum) AS over03, sum(B.h4_overnum) AS over04, sum(B.h5_overnum) AS over05, sum(B.h6_overnum) AS over06, su
11g里面用listagg: select listagg(name,',') within (order by id) from table 10g里面用wm_concat:select wm_concat(name) from table wm_concat是undocument的listagg是11g document的
with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Shanghai' city from dual union all select 'China' nation ,'Beijing' city from dual union all select 'USA' nation ,'New York' city from dual union all se