--列轉行,逗號拼接指定列的值
sql server中寫法:
select stuff(( select ',' + field1 from tablea for xml path('')), 1, 1, '')
oracle中寫法:
方法一:wmsys.wm_concat
select wmsys.wm_concat(field1) from tablea
方法二:listagg()
2.1、listagg(***,***) within group( order by ***)
用法就像聚合函式一樣,通過
group by語句,把每個group的乙個字段,拼接起來
1 with temp as(2select
'china
' nation,'
beijing
' city from
dual union
3select
'china
' nation,'
shanghai
' city from
dual union
4select
'china
' nation,'
guangzhou
' city from
dual union
5select
'usa
' nation,'
new york
' city from
dual union
6select
'usa
' nation,'
bostom
' city from
dual7)
8select nation,listagg(city,','
) within group(order by city)
9from
temp
10 group by nation;
2.2、over(partition by ***)
在不使用group by語句時候,使用listagg函式(
當作sum()函式來使用)
1 with temp as(2select
'china
' nation,'
beijing
' city from
dual union
3select
'china
' nation,'
shanghai
' city from
dual union
4select
'china
' nation,'
guangzhou
' city from
dual union
5select
'usa
' nation,'
new york
' city from
dual union
6select
'usa
' nation,'
bostom
' city from
dual7)
8select nation,city,listagg(city,','
) within group(order by city) over(partition by nation) rank
9from temp;
如果您看了本篇部落格,覺得對您有所收穫,右下角的[推薦]
sql多方法實現列轉行
create table studentscore2 username nvarchar 50 語文 float,數學 float,英語 float,物理 float insert into studentscore2 username,語文 數學 英語 物理 values helen 100,90...
python 列轉行 SQL 行轉列,列轉行
sql 行轉列,列轉行 行列轉換在做報表分析時還是經常會遇到的,今天就說一下如何實現行列轉換吧。行列轉換就是如下圖所示兩種展示形式的互相轉換 行轉列假如我們有下表 select from student pivot sum score for subject in 語文,數學,英語 通過上面 sql...
sql中 列轉行
列轉行,主要是通過union all max來實現。假如有下面這麼乙個表 createtableprogrectdetail progrectname nvarchar 20 工程名稱 overseasupplyint,海外 商供給數量 nativesupply int,國內 商供給數量 south...