`
goon
  • 浏览: 181289 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

oracle 行列转换《转》

阅读更多
网络上有很多,但这篇做法写的比较简单、清楚。
test:
NAME
KM
CJ
张三
语文
80
张三
数学
86
张三
英语
75
李四
语文
78
李四
数学
85
李四
英语
79
test2:
NAME
语文
数学
英语
张三
80
86
75
李四
78
85
79
行转列sql ------test->test2
SELECT NAME,
       SUM(decode(t.km, '语文', t.cj, 0)) 语文,
       SUM(decode(t.km, '数学', t.cj, 0)) 数学,
       SUM(decode(t.km, '英语', t.cj, 0)) 英语
  FROM test t
 GROUP BY t.name
列转行sql ------test2->test
select t.name,'语文' AS km,t.语文 AS cj from test2 t
UNION ALL
select t.name,'数学' AS km,t.数学 AS cj from test2 t
UNION ALL
select t.name,'英语' AS km,t.英语 AS cj from test2 t
ORDER BY name
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics