日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle非常量不能用于privot_Oracle 行列转换函数pivot、unpivot的使用(二)

發(fā)布時間:2024/9/27 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle非常量不能用于privot_Oracle 行列转换函数pivot、unpivot的使用(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、行轉(zhuǎn)列pivot

關(guān)鍵函數(shù)pivot,其用法如下 pivot(聚合函數(shù) for 列名 in(類型))

select * from table_name pivot(max(column_name)??????????????????????????? --行轉(zhuǎn)列后的列的值value,聚合函數(shù)是必須要有的

for column_name in(value_1,value_2,value_3)???? --需要行轉(zhuǎn)列的列及其對應列的屬性1/2/3

)

1、首先舉一個簡單的例子,創(chuàng)建一個數(shù)據(jù)表

create table tmp as select * from (

select '張三' student,'語文' course ,78 score from dual union all

select '張三','數(shù)學',87 from dual union all

select '張三','英語',82 from dual union all

select '張三','物理',90 from dual union all

select '李四','語文',65 from dual union all

select '李四','數(shù)學',77 from dual union all

select '李四','英語',65 from dual union all

select '李四','物理',85 from dual);

先使用decode或case when方法

select

student,

max(decode(course, '語文', score)) 語文,

max(decode(course, '數(shù)學', score)) 數(shù)學,

max(decode(course, '英語', score)) 英語,

max(decode(course, '物理', score)) 物理,

sum(score) total

from tmp

group by student;

-----------------------------------------

select

student,

max(case when course = '語文' then score end) 語文,

max(case when course = '數(shù)學' then score end) 數(shù)學,

max(case when course = '英語' then score end) 英語,

max(case when course = '物理' then score end) 物理,

sum(score) total

from tmp

group by student;

pivot的使用

select t.*,

(t.語+t.數(shù)+t.外+t.物) as total

from

(select *

from tmp pivot ( max(score) for course in ('語文' as 語 , '數(shù)學' as 數(shù), '英語' as 外,'物理' as 物) )

) t;

結(jié)果同上

2、實際開發(fā)遇到的問題

有一張目標值表,年、月、日的值都是分開多行顯示,現(xiàn)需合并成一行顯示,具體數(shù)據(jù)如下:(type:1-->日,2-->月,3-->年;targetvalue:目標值)

select * from MOVEBI.T_GMS_MBI_TARGET_DATA where targetcode = '31227061'

此數(shù)據(jù)必須先進性處理,要保證數(shù)據(jù)可以聚合成一條,若直接使用會出現(xiàn)下列情況:

select * from MOVEBI.T_GMS_MBI_TARGET_DATA pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '';

這不是我們想要的結(jié)果,具體改進法法如下:

--方法一:對結(jié)果處理

select max(datatime) datatime

,usercode

,deptcode

,deptname

,targetcode

,targetname

,sum(coalesce(day_value,0)) day_value

,sum(coalesce(mon_value,0)) mon_value

,sum(coalesce(year_value,0)) year_value

from(

select datatime,usercode,deptcode,deptname,targetcode,targetname,day_value,mon_value,year_value

from MOVEBI.T_GMS_MBI_TARGET_DATA

pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '')

group by usercode

,deptcode

,deptname

,targetcode

,targetname;

--方法二:對原始表處理

select *

from (select '' datatime,

usercode,

deptcode,

deptname,

targetcode,

targetname,

targetvalue,

type

from MOVEBI.T_GMS_MBI_TARGET_DATA

where datatime in ('', '')

and targetcode = '') t

pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '';

二、列轉(zhuǎn)行unpivot

根據(jù)上面的例子創(chuàng)建tmp_2測試用表

select student,科目,成績 from tmp_2 unpivot (成績 for 科目 in (語文, 數(shù)學, 英語, 物理));

同樣不使用unpivot也可以實現(xiàn)同樣的效果,只是sql語句會很長,而且執(zhí)行速度效率也沒有前者高

select student,'語文' 科目, (select 語文 from tmp_2 where student=f.student) 成績 from tmp_2 f

union

select student,'數(shù)學' 科目, (select 數(shù)學 from tmp_2 where student=f.student) 成績 from tmp_2 f

union

select student,'英語' 科目, (select 英語 from tmp_2 where student=f.student) 成績 from tmp_2 f

union

select student,'物理' 科目, (select 物理 from tmp_2 where student=f.student) 成績 from tmp_2 f

-------------------------------------------

select student,'語文' 科目,語文 from tmp_2

union

select student,'數(shù)學' 科目,語文 from tmp_2

union

select student,'英語' 科目,語文 from tmp_2

union

select student,'物理' 科目,語文 from tmp_2

(注:此為學習記錄筆記,僅供參考若有問題請指正,后續(xù)補充......)

參考文檔:https://blog.csdn.net/xiaokui_wingfly/article/details/42419207

參考文檔:https://www.cnblogs.com/harvey888/p/6735093.html

參考文檔:https://www.cnblogs.com/markfeifei/p/4009343.html

oracle行列轉(zhuǎn)換函數(shù)的使用

oracle 10g wmsys.wm_concat行列轉(zhuǎn)換函數(shù)的使用: 首先讓我們來看看這個神奇的函數(shù)wm_concat(列名),該函數(shù)可以把列值以","號分隔起來,并顯示成一行 ...

Oracle11g 行列轉(zhuǎn)換函數(shù)PIVOT and UNPIVOT

作為Oracle開發(fā)工程師,推薦大伙看看 PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1 This article shows ...

oracle 行列轉(zhuǎn)換函數(shù)之WM_CONCAT和LISTAGG的使用(一)

一.wm_concat函數(shù) wm_concat能夠?qū)崿F(xiàn)同樣的功能,但是有時在11g中使用需要用to_char()進行轉(zhuǎn)換,否則會出現(xiàn)不兼容現(xiàn)象(WMSYS.WM_CONCAT: 依賴WMSYS 用戶, ...

Oracle行列轉(zhuǎn)換

一.建表與插入數(shù)據(jù) 1.1.建表 create table kecheng ( id NUMBER, name ), course ), score NUMBER ); insert into kec ...

oracle 行列轉(zhuǎn)換

oracle 行列轉(zhuǎn)換列名如果是數(shù)字,用雙引號包住? 如下: --? 建表 create table workinfo(wid integer primary key,sid integer ,CON ...

Oracle 大小寫轉(zhuǎn)換函數(shù)

Oracle?大小寫轉(zhuǎn)換函數(shù) 轉(zhuǎn)大寫UPPER 轉(zhuǎn)小寫LOWER 測試: select UPPER('Test') as u from dual; select LOWER('Test') as l ...

SQL(橫表和縱表)行列轉(zhuǎn)換,PIVOT與UNPIVOT的區(qū)別和使用方法舉例,合并列的例子

使用過SQL Server 2000的人都知道,要想實現(xiàn)行列轉(zhuǎn)換,必須綜合利用聚合函數(shù)和動態(tài)SQL,具體實現(xiàn)起來需要一定的技巧,而在SQL Server 2005中,使用新引進的關(guān)鍵字PIVOT/UN ...

行列轉(zhuǎn)換小結(jié) Pivot ,Unpivot (轉(zhuǎn),改)

行專列 Pivot 1)SQL 2000版本 靜態(tài) SELECT ID , SUM(CASE Code WHEN 'Item1' THEN Value END) AS Item1 , SUM(CASE ...

Oracle行列轉(zhuǎn)換的思考與總結(jié)

最近幾天一直在弄Oracle-SQL的問題,涉及到了一些平時沒有用到的東西,也因此而在這里郁悶了好久.現(xiàn)在問題得到了解決雖說不算完美.但是還是和大家一起分享一下. 行列轉(zhuǎn)換之一:sum(case wh ...

隨機推薦

how to javafx hide background header of a tableview?

http://stackoverflow.com/questions/12324464/how-to-javafx-hide-background-header-of-a-tableview ———— ...

c#的方法重寫和的java方法重寫有什么區(qū)別

java code: package example; class m1 { public int getInt() { return 0; } } class m2 extends m1 { pub ...

cocos2d-x plist+wen使用

http://zhan.renren.com/tag?value=cocos2dx&from=template http://blog.csdn.net/zhanglongit/article ...

第三周博客之二---Oracle中的sql語句

一.用戶及權(quán)限(DBA有最高系統(tǒng)權(quán)限) 1.數(shù)據(jù)庫的安全性:系統(tǒng)的安全性.數(shù)據(jù)的安全性 2.權(quán)限分類: 2.1系統(tǒng)權(quán)限:獲得后可訪問數(shù)據(jù)庫 常用的有create table,create user,c ...

python爬蟲之pyquery學習

相關(guān)內(nèi)容: pyquery的介紹 pyquery的使用 安裝模塊 導入模塊 解析對象初始化 css選擇器 在選定元素之后的元素再選取 元素的文本.屬性等內(nèi)容的獲取 pyquery執(zhí)行DOM操作.css ...

JAVA 設計模式遵循的六大基本準則

JAVA 設計模式遵循的六大基本準則 一.單一職責原則:(Single Responsibility Pinciple) ?一個類只負責一項職責. 當超過一項職責需要負責時,需要增加新的類來負責新的職 ...

[洛谷 P3787] 冰精凍西瓜

題目描述 琪露諾是擁有操縱冷氣程度的能力的妖精,一天她發(fā)現(xiàn)了一片西瓜地.這里有n個西瓜,由n-1條西瓜蔓連接,形成一個有根樹,琪露諾想要把它們冷凍起來慢慢吃. 這些西瓜蔓具有神奇的性質(zhì),可以將經(jīng)過它的 ...

高性能JavaScript(快速響應的用戶界面)

瀏覽器UI線程 用于執(zhí)行JavaScript代碼和更新界面的進程被稱為 “瀏覽器UI線程” . UI線程的工作基于一個簡單的隊列系統(tǒng),任務會被保存到隊列中直到線程空閑,一旦空閑隊列就被重新提取出來運行 ...

總結(jié)

以上是生活随笔為你收集整理的oracle非常量不能用于privot_Oracle 行列转换函数pivot、unpivot的使用(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。