锁定老帖子 主题:一道有趣的sql题2
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (3)
|
|
---|---|
作者 | 正文 |
发表时间:2011-04-12
最后修改:2011-04-12
真正的列转行是要实现以下功能,大家一起动脑思考吧。 year columnA columnB 2009 200 300 2010 300 400 变成 column year data columnA 2009 200 columnA 2010 300 columnB 2009 300 columnB 2010 400 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-04-12
create table my_test(
year varchar2(100), columnA varchar2(100), columnB varchar2(100) ); insert into my_test values('2009','200','300'); insert into my_test values('2010','300','400'); SELECT a.aa, a.YEAR, ca DATA FROM (SELECT 'columna' aa, YEAR, columna ca FROM my_test) a, (SELECT 'columnb', YEAR, columnb cb FROM my_test) b WHERE a.YEAR = b.YEAR UNION ALL SELECT b.bb, b.YEAR, cb DATA FROM (SELECT 'columna' aa, YEAR, columna ca FROM my_test) a, (SELECT 'columnb' bb, YEAR, columnb cb FROM my_test) b WHERE a.YEAR = b.YEAR; |
|
返回顶楼 | |
发表时间:2011-04-12
还有,别说网上找不到列转行的例子!多的是!
|
|
返回顶楼 | |
发表时间:2011-04-12
SELECT a.col , a.YEAR, a.columna DATA
FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR UNION ALL SELECT b.col, b.YEAR, B.COLUMNB DATA FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR; 整理了一下别名,貌似column不能用作列名 |
|
返回顶楼 | |
发表时间:2011-04-12
biaobiao520 写道 SELECT a.col , a.YEAR, a.columna DATA
FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR UNION ALL SELECT b.col, b.YEAR, B.COLUMNB DATA FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR; 整理了一下别名,貌似column不能用作列名 受教了 |
|
返回顶楼 | |
发表时间:2011-04-12
yangguo 写道 biaobiao520 写道 SELECT a.col , a.YEAR, a.columna DATA
FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR UNION ALL SELECT b.col, b.YEAR, B.COLUMNB DATA FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR; 整理了一下别名,貌似column不能用作列名 受教了 晕。。。 好像直接这么取就可以了。。。 SELECT a.col , a.YEAR, a.columna DATA FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a UNION ALL SELECT b.col, b.YEAR, B.COLUMNB DATA FROM (SELECT 'columnb' col, YEAR, columnb FROM my_test) b |
|
返回顶楼 | |
发表时间:2011-04-12
biaobiao520 写道 yangguo 写道 biaobiao520 写道 SELECT a.col , a.YEAR, a.columna DATA
FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR UNION ALL SELECT b.col, b.YEAR, B.COLUMNB DATA FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a, (SELECT 'columnb' col, YEAR, columnb FROM my_test) b WHERE a.YEAR = b.YEAR; 整理了一下别名,貌似column不能用作列名 受教了 晕。。。 好像直接这么取就可以了。。。 SELECT a.col , a.YEAR, a.columna DATA FROM (SELECT 'columna' col, YEAR, columna FROM my_test) a UNION ALL SELECT b.col, b.YEAR, B.COLUMNB DATA FROM (SELECT 'columnb' col, YEAR, columnb FROM my_test) b 哈哈,我刚发短信给你,也是这样。 |
|
返回顶楼 | |
发表时间:2011-04-12
= =!如果是Oracle用下函数就行了。
|
|
返回顶楼 | |
发表时间:2011-04-12
java_jing 写道 = =!如果是Oracle用下函数就行了。
什么函数? |
|
返回顶楼 | |
发表时间:2011-04-12
楼主, 建议把题目改成任意个Column的列转行,不然得出来的解决方案不怎么通用。
比如改成一百个Column的列转行, 如果用Union All,写死你。 |
|
返回顶楼 | |