阅读 10715 次
发表时间: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


发表时间: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,写死你。
Global site tag (gtag.js) - Google Analytics