從Oracle 表格行列轉(zhuǎn)置說起
更新時(shí)間:2009年09月21日 18:35:22 作者:
當(dāng)你面對如下格式的一個(gè)表格:NO為人員的ID,MONEY是收入,DAY是星期(1代表星期一,7代表周日)。
decode()函數(shù)使用技巧
·軟件環(huán)境:
1、Windows NT4.0+ORACLE 8.0.4
2、ORACLE安裝路徑為:C:\ORANT
·含義解釋:
decode(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)
該函數(shù)的含義如下:
IF 條件=值1 THEN
RETURN(翻譯值1)
ELSIF 條件=值2 THEN
RETURN(翻譯值2)
......
ELSIF 條件=值n THEN
RETURN(翻譯值n)
ELSE
RETURN(缺省值)
END IF
· 使用方法:
1、比較大小
select decode(sign(變量1-變量2),-1,變量1,變量2) from dual; --取較小值
sign()函數(shù)根據(jù)某個(gè)值是0、正數(shù)還是負(fù)數(shù),分別返回0、1、-1
例如:
變量1=10,變量2=20
則sign(變量1-變量2)返回-1,decode解碼結(jié)果為“變量1”,達(dá)到了取較小值的目的。
2、表、視圖結(jié)構(gòu)轉(zhuǎn)化
現(xiàn)有一個(gè)商品銷售表sale,表結(jié)構(gòu)為:
month char(6) --月份
sell number(10,2) --月銷售金額
現(xiàn)有數(shù)據(jù)為:
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
想要轉(zhuǎn)化為以下結(jié)構(gòu)的數(shù)據(jù):
year char(4) --年份
month1 number(10,2) --1月銷售金額
month2 number(10,2) --2月銷售金額
month3 number(10,2) --3月銷售金額
month4 number(10,2) --4月銷售金額
month5 number(10,2) --5月銷售金額
month6 number(10,2) --6月銷售金額
month7 number(10,2) --7月銷售金額
month8 number(10,2) --8月銷售金額
month9 number(10,2) --9月銷售金額
month10 number(10,2) --10月銷售金額
month11 number(10,2) --11月銷售金額
month12 number(10,2) --12月銷售金額
結(jié)構(gòu)轉(zhuǎn)化的SQL語句為:
create or replace view
v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
as
select
substrb(month,1,4),
sum(decode(substrb(month,5,2),'01',sell,0)),
sum(decode(substrb(month,5,2),'02',sell,0)),
sum(decode(substrb(month,5,2),'03',sell,0)),
sum(decode(substrb(month,5,2),'04',sell,0)),
sum(decode(substrb(month,5,2),'05',sell,0)),
sum(decode(substrb(month,5,2),'06',sell,0)),
sum(decode(substrb(month,5,2),'07',sell,0)),
sum(decode(substrb(month,5,2),'08',sell,0)),
sum(decode(substrb(month,5,2),'09',sell,0)),
sum(decode(substrb(month,5,2),'10',sell,0)),
sum(decode(substrb(month,5,2),'11',sell,0)),
sum(decode(substrb(month,5,2),'12',sell,0))
from sale
group by substrb(month,1,4);
相關(guān)文章
oracle數(shù)據(jù)庫ORA-01196錯(cuò)誤解決辦法分享
這篇文章主要介紹了oracle數(shù)據(jù)庫ORA-01196錯(cuò)誤解決辦法分享,小編覺得還是挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10
在客戶端配置TNS測試報(bào)錯(cuò)ORA-12170:TNS:連接超時(shí)
在Red Hat Enterprise Linux Server Releae 5.5 成功安裝ORACLE 10g 后,在客戶端配置TNS后,測試是否可以連接到數(shù)據(jù)塊服務(wù)器,結(jié)果報(bào)錯(cuò):ORA-12170:TNS:連接超時(shí)2012-12-12
Oracle數(shù)據(jù)庫連接并訪問Microsoft?SQL?Server數(shù)據(jù)庫操作步驟
近期在項(xiàng)目中客戶軟件升級,舊版本的數(shù)據(jù)庫用的SQL Server而新版本換為了Oracle,這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫連接并訪問Microsoft?SQL?Server數(shù)據(jù)庫的操作步驟,需要的朋友可以參考下2024-08-08
WMware redhat 5 oracle 11g 安裝方法
本文將詳細(xì)介紹WMware中redhat 5 安裝oracle 11g方法,需要的朋友可以參考下2012-12-12
oracle下一條SQL語句的優(yōu)化過程(比較詳細(xì))
很簡單的一次調(diào)整,語句加了適當(dāng)?shù)乃饕笮阅芫陀写蠓奶嵘.?dāng)時(shí)看到這條語句的時(shí)候,第一感覺就是執(zhí)行效率肯定低下。語句的功能是求某一客戶當(dāng)天產(chǎn)品的總銷量。2010-04-04
PL/SQL?Developer15和Oracle?Instant?Client安裝配置詳細(xì)圖文教程
PL/SQL Developer是一種集成的開發(fā)環(huán)境,專門用于開發(fā)、測試、調(diào)試和優(yōu)化Oracle PL/SQL存儲程序單元,比如觸發(fā)器等,這篇文章主要給大家介紹了關(guān)于PL/SQL?Developer15和Oracle?Instant?Client安裝配置的詳細(xì)圖文教程,需要的朋友可以參考下2024-04-04
CentOS8下安裝oracle客戶端完整(填坑)過程分享(推薦)
這篇文章主要介紹了CentOS8下安裝oracle客戶端完整(填坑)過程分享,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

