oracle 豎表,橫表(a stupid way,want the smart way)
豎表->橫表
select a.user_ohr_id sso, max(decode(a.change_type_id,1, b.change_type_desc,'')) t1, max(decode(a.change_type_id,2, b.change_type_desc,'')) t2, max(decode(a.change_type_id,3, b.change_type_desc,'')) t3, max(decode(a.change_type_id,4, b.change_type_desc,'')) t4, max(decode(a.change_type_id,5, b.change_type_desc,'')) t5, max(decode(a.change_type_id,6, b.change_type_desc,'')) t6, max(decode(a.change_type_id,7, b.change_type_desc,'')) t7 from occr_user_changetype a ,occr_change_type b where a.change_type_id = b.change_type_id group by a.user_ohr_id
橫表->豎表
select change_type_id,change_type_desc from occr_change_type
union all
select change_type_id,change_type_detail from occr_change_type
order by change_type_id
another way:(complex)
Create table test (name char(10),km char(10),cj int)
go
insert test values('張三','語文',80)
insert test values('張三','數學',86)
insert test values('張三','英語',75)
insert test values('李四','語文',78)
insert test values('李四','數學',85)
insert test values('李四','英語',78)
想變成
姓名 語文 數學 英語
張三 80 86 75
李四 78 85 78
declare @sql varchar(8000)
set @sql = 'select name'
select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']'
from (select distinct km from test) as a
select @sql = @sql+' from test group by name'
exec(@sql)
drop table test
drop table table1
go
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/106285/viewspace-1011730/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 豎錶轉橫表(ORACLE)Oracle
- 提問的智慧How To Ask Questions The Smart Way
- 豎錶轉橫表(SQL SERVER)SQLServer
- The Way to HappinessAPP
- The Way To Go --- 切片Go
- a-better-way-to-find-literal-sqls-in-oracle-10g/SQLOracle
- 演算法之路 - Way to Algorithm演算法Go
- POJ 2891 Strange Way to Express IntegersExpress
- Learning Django: the hard way (1)Django
- New way to deploy SSL VPN in LinuxLinux
- The Best Way to Export an SVG from SketchExportSVG
- Get table and index DDL in the easy way(轉)Index
- Python: Choice of the one obvious way to do itPython
- Objectify: A Better Way to Build Rails ApplicationsObjectUIAIAPP
- 推薦 | 通往AGI之路(Way to AGI)
- CF 2B The least round way(DP)AST
- iOS橫豎屏iOS
- One simple way to draw canvas, wxml2canvasCanvasXML
- nohup not working, another way to get the script run in the background
- POJ 2891 Strange Way to Express Integers(擴充套件GCD)Express套件GC
- activity橫屏豎屏
- perl橫豎轉換
- 《Learn python the hard way》Exercise 48: Advanced User InputPython
- Strange Way to Express Integers(中國剩餘定理+不互質)Express
- Learn Linux The Hard Way/笨辦法學LinuxLinux
- iOS 橫豎屏切換iOS
- SAP UI5控制元件資料繫結的三種模式 - One Way, Two Way和OneTime實現原理比較UI控制元件模式
- Learn Jenkins the hard way (3) - Jenkins的儲存模型Jenkins模型
- One more way regarding germany niubian how to remain difficult forREMAI
- Android橫豎屏切換Android
- No way to dispatch this command to Redis Cluster because keys have different slots.Redis
- 一個疑惑 ,gc cr grant 2-way ,簡單記錄GC
- iOS 橫豎屏旋轉總結iOS
- App中橫豎屏的設定APP
- 檢測橫屏豎屏程式碼
- The Data Way Vol.7|從故事裡尋找開源的『核心』
- The Data Way Vol.6|我不是開發者,但我依然嚮往開源
- POJ 2891-trange Way to Express Integers(解線性同餘方程組)Express