最近專案中匯出資料一直用到將多行資料合併成一列顯示,以前不太會用,最近看到網上有一些方法,總結一下以後備用:
有乙個表,表名為tab_test,如下:
一、多行轉一列
select id, vnum, to_char(wmsys.wm_concat(vname)) c
from tab_test group by id,vnum;
執行上述sql後,如下:
二、一列轉多行
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
select 'usa' nation ,'bostom' city from dual union all
select 'japan' nation ,'tokyo' city from dual
) select nation,listagg(city,',') within group (order by city)
from temp
group by nation
withtempas(
select500 population, 'china' nation ,'guangzhou' cityfromdualunionall
select1500 population, 'china' nation ,'shanghai' cityfromdualunionall
select500 population, 'china' nation ,'beijing' cityfromdualunionall
select1000 population, 'usa' nation ,'new york' cityfromdualunionall
select500 population, 'usa' nation ,'bostom' cityfromdualunionall
select500 population, 'japan' nation ,'tokyo' cityfromdual
)
selectpopulation,
nation,
city,
listagg(city,',') withingroup(orderbycity) over (partitionbynation) rank
fromtemp
oracle 多行轉一列
以前用過又忘了。今天做資料匯出用到,記下。oracle中的wmsys.wm concat主要實現行轉列功能 說白了就是將查詢的某一列值使用逗號進行隔開拼接,成為一條資料 select d.id,d.papid as 援助系統,i.papname,d.fid as 發貨單號,d.fwdate as 發...
SQL 多行轉一列
1 建立表 create table dbo a id nchar 10 collate chinese prc ci as null,name nchar 10 collate chinese prc ci as null,value nchar 10 collate chinese prc ci...
一列資料轉多行多列
一列資料轉多行多列 方法二 插入行列公式 公式解析 row 的功能是返回引用的行號 column 的功能是返回引用的列號 indirect函式 功能 返回文字字串所指定的引用 語法規則 indirect ref text,a1 ref text 單元格引用,該引用所指向的單元格中存放有對另一單元格的...