1. 程式人生 > >Oracle字串,行轉列、列轉行的Sql語句總結

Oracle字串,行轉列、列轉行的Sql語句總結

多行轉字串

這個比較簡單,用||或concat函式可以實現

 SQL Code 

1
2

select concat(id,username) str from app_user
select id||username str from app_user

字串轉多列

實際上就是拆分字串的問題,可以使用 substr、instr、regexp_substr函式方式

字串轉多行

使用union all函式等方式

wm_concat函式

首先讓我們來看看這個神奇的函式wm_concat(列名),該函式可以把列值以","號分隔起來,並顯示成一行,接下來上例子,看看這個神奇的函式如何應用準備測試資料

 SQL Code 

1
2
3
4
5
6

create table test(id number,name varchar2(20));
insert into test values(1,'a');
insert into test values(1,'b');
insert into test values(1,'c');
insert into test values(2,'d');
insert into test values(2,'e');

效果1 : 行轉列 ,預設逗號隔開

 SQL Code 

1

select wm_concat(name) name from test;

效果2: 把結果裡的逗號替換成"|"

 SQL Code 

1

select replace(wm_concat(name),',','|'from test;

效果3: 按ID分組合並name

 SQL Code 

1

select id,wm_concat(name) name from test group by id;

sql語句等同於下面的sql語句:

  SQL Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

-------- 適用範圍:8i,9i,10g及以後版本 MAX + DECODE 
select id,
       max(decode(rn, 1, name, null)) ||
       max(decode(rn, 2',' || name, null)) ||
       max(decode(rn, 3',' || name, null)) str
  from (select id,
               name,
               row_number() over(partition by id order by name) as rn
          from test) t
 group by id
 order by 1;
-------- 

適用範圍:8i,9i,10g及以後版本 ROW_NUMBER + LEAD 
select id, str
  from (select id,
               row_number() over(partition by id order by name) as rn,
               name || lead(',' || name, 1) over(partition by id order by name) ||
               lead(',' || name, 2) over(partition by id order by name) || 
               lead(',' || name, 3) over(partition by id order by name) as str
          from test)
 where rn = 1
 order by 1;
-------- 
適用範圍:10g及以後版本 MODEL 
select id, substr(str, 2) str
  from test model return updated rows partition by(id) dimension by(row_number() 
  over(partition by id order by name) as rn) measures(cast(name as varchar2(20)) as str) 
  rules upsert iterate(3) until(presentv(str [ iteration_number + 2 ], 10) = 0)
  (str [ 0 ] = str [ 0 ] || ',' || str [ iteration_number + 1 ])
 order by 1;
-------- 
適用範圍:8i,9i,10g及以後版本 MAX + DECODE 
select t.id id, max(substr(sys_connect_by_path(t.name, ','), 2)) str
  from (select id, name, row_number() over(partition by id order by name) rn
          from test) t
 start with rn = 1
connect by rn = prior rn + 1
       and id = prior id
 group by t.id;

懶人擴充套件用法:

案例: 我要寫一個檢視,類似"create or replace view as select 欄位1,...欄位50 from tablename" ,基表有50多個欄位,要是靠手工寫太麻煩了,有沒有什麼簡便的方法? 當然有了,看我如果應用wm_concat來讓這個需求變簡單,假設我的APP_USER表中有(id,username,password,age)4個欄位。查詢結果如下

 SQL Code 

1
2
3
4
5

/** 這裡的表名預設區分大小寫 */
select 'create or replace view as select ' || wm_concat(column_name) ||
       ' from APP_USER' sqlStr
  from user_tab_columns
 where table_name = 'APP_USER';

利用系統表方式查詢

 SQL Code 

1

select * from user_tab_columns

Oracle 11g 行列互換 pivot 和 unpivot 說明

在Oracle 11g中,Oracle 又增加了2個查詢:pivot(行轉列) 和unpivot(列轉行)

參考:http://blog.csdn.net/tianlesoftware/article/details/7060306、http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html 

google 一下,網上有一篇比較詳細的文件:http://www.oracle-developer.net/display.php?id=506

pivot 列轉行

測試資料 (id,型別名稱,銷售數量),案例:根據水果的型別查詢出一條資料顯示出每種型別的銷售數量。

 SQL Code 

1
2
3
4
5
6
7
8
9

create table demo(id int,name varchar(20),nums int);  ---- 建立表
insert into demo values(1'
蘋果'1000);
insert into demo values(2'
蘋果'2000);
insert into demo values(3'
蘋果'4000);
insert into demo values(4'
橘子'5000);
insert into demo values(5'
橘子'3000);
insert into demo values(6'
葡萄'3500);
insert into demo values(7'
芒果'4200);
insert into demo values(8'
芒果'5500);

分組查詢 (當然這是不符合查詢一條資料的要求的)

 SQL Code 

1

select name, sum(nums) nums from demo group by name

行轉列查詢

 SQL Code 

1

select * from (select name, nums from demo) pivot (sum(nums) for name in ('蘋果'蘋果'橘子''葡萄''芒果'));

注意: pivot(聚合函式 for 列名 in(型別)) ,其中 in('') 中可以指定別名,in中還可以指定子查詢,比如 select distinct code from customers

當然也可以不使用pivot函式,等同於下列語句,只是程式碼比較長,容易理解

相關推薦

Oracle字串轉行Sql語句總結

多行轉字串這個比較簡單,用||或concat函式可以實現 SQL Code 12select concat(id,username) str from app_userselect id||username str from app_user字串轉多列實際上就是拆分字串的問題

oracle轉行連續日期數字實現方式及mybatis下實現方式

九月份複習,十月份考試,十月底一直沒法收心,趕在十一初 由於不可抗拒的原因又不得不重新找工作就;欸~, 又是一番折騰,從入職到現在,可又沒法閒下來了... 這次就簡單介紹下oracle資料庫下如何實現行轉列、列轉行及此在mybatis中的實現方式,就具體用法我就不詳細說了,主要介紹下實戰中所碰到的坑

Oracle pivot 轉行unpivot 的Sql語句總結

多行轉字串 這個比較簡單,用||或concat函式可以實現 print? 1.  select concat(id,username) str from app_user   2.     3.  select id||username str from app_use

oracle轉行

行轉列:PIVOT列轉行:UNPIVOT這兩個是在oracle11g上面新增的函式。下面舉例說明用法。PIVOT:學生成績表,原資料:select class_name, student_name, course_type, result, created_date fr

Oracle SQL函式pivotunpivot置函式實現轉行

函式PIVOT、UNPIVOT轉置函式實現行轉列、列轉行,效果如下圖所示: 1.PIVOT為行轉列,從圖示的左邊到右邊 2.UNPIVOT為列轉行,從圖示的右邊到左邊 3.左邊為縱表,結構簡單,易擴充套件 4.右邊為橫表,展示清晰,方便查詢 5.很多時候業務表為縱表,但是統

MySQL的轉行連線字串 concatconcat_wsgroup_concat函式用法

1.concat函式 使用方法: CONCAT(str1,str2,…) 返回結果為連線引數產生的字串。如有任何一個引數為NULL ,則返回值為 NULL。 注意: 如果所有引數均為非二進位制字串,則結果為非二進位制字串。 如果自變

oracle資料錶

今天看到一個題目,要求是行轉列,回來查了一下資料 題目要求是把 這樣的一張錶轉換為 這樣的表格 首先建立表 CREATE TABLE A ( year VARCHAR2(255), month VARCHAR2(255), amount NUMBER ); 然後插入資料 INSE

使用case when,union all實現sql轉行

-- 建表 CREATE TABLE StudentScores ( UserName NVARCHAR(20), -- 學生姓名 Subject

PIVOT函式

PIVOT函式的格式如下 PIVOT(<聚合函式>([聚合列值]) FOR [行轉列前的列名] IN([行轉列後的列名1],[行轉列後的列名2],[行轉列後的列名3],.......[行轉列後的列名N])) <聚合函式>就是我們使用的SUM,COUNT,AVG等

oracle 查詢結果

工作中遇到了這麼一個情況:sql查詢結果,查出來需要遍歷才能取到想要的結果 SELECT LAYER_CODE,GROUP_NAME FROM BS_CHNGROUP_DICT START WITH GROUP_ID = '10809' CONNECT

關於oracle的一個案例

如下圖所示 第一個資料表經過sql語句需要轉換成第二個資料庫表 方法一: 採用case when 轉換 sql語句:select name, max(case course when ‘JDBC’ then grade else 0 end) jdbc, max( c

SQL Server 轉行。多成一

一、多行轉成一列(並以","隔開)表名:A表資料:想要的查詢結果:查詢語句:SELECT name , value = ( STUFF(( SELECT ',' + value FROM A

轉行題目(收集中...)

一、列轉行 1、把如下表 year  month amount 1991   1     1.1 1991   2     1.2 1991   3     1.3 1991   4     1.4

資料庫欄位存JSON何不弄張表

今天對專案進行優化,原有的資料庫中,有一張表A,裡面有個欄位,存了一段JSON格式配置引數,可能會很長。頁面端要對這個表進行展示和管理。那麼在管理這個欄位的時候就比較麻煩了,要把JSON轉成List放到頁面遍歷,儲存時再轉成JSON。楊老提出說搞一張表,同一個主鍵對就多個J

SQL轉行

SQL行轉列、列轉行 這個主題還是比較常見的,行轉列主要適用於對資料作聚合統計,如統計某類目的商品在某個時間區間的銷售情況。列轉行問題同樣也很常見。 一、整理測試資料 create table wyc

mysql-轉行

group_concat(),函式說明     手冊上說明:該函式返回帶有來自一個組的連線的非NULL值的字串結果;     通俗點理解,其實是這樣的:group_concat()會計算哪些行屬於同一組,將屬於同一組的列顯示出來。要返回哪些列,由函式引數(就是欄位名)決定。分組必須有個標準,就是根據grou

刪除重複資料轉行

查詢是否有重複資料http://write.blog.csdn.net/postedit/53517081 select name,conunt(*) from test group by name having count(*)>1; 刪除重複資料 delete a

Databricks 第11篇:Spark SQL 查詢(轉行Lateral View排序)

本文分享在Azure Databricks中如何實現行轉列和列轉行,並介紹對查詢的結果進行排序的各種方式。 一,行轉列 在分組中,把每個分組中的某一列的資料連線在一起: collect_list:把一個分組中的列合成為陣列,資料不去重,格式是['a','a','b'] collect_set:把一個分組中的

GreenPlum之數組合並取交集及函數

blog ner fun cnblogs $1 $$ ble lec temp --1.利用INTERSECT關鍵字數組之間交集函數 CREATE OR REPLACE FUNCTION array_intersect(anyarray, anyarray) RETU

SQL成一

CREATE TABLE [dbo].[a]([id] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL,[name] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL,[value] [nchar]