假设这个表的3个列分别是 col_a,col_b,col_c,那么sql为
select distinct col_final from (select distinct col_a as col_final from table_nameunion allselect distinct col_b as col_final from table_nameunion allselect distinct col_c as col_final from table_name) t order by col_final
然后再转置.没有测试该SQL
用子查询关联.
假设table1 有三个字段 col1, col2, col3. 有三条记录 (a,b,c),(a,b,d),(a,b,e).
SQL如下:
select sq1.*, sq2.col3, sq3.col3
from
(select * from table1 where a.col1='a' and a.col2='b' and a.col3='c' ) sq1,
(select * from table1 where a.col1='a' and a.col2='b' and a.col3='d' ) sq2,
(select * from table1 where a.col1='a' and a.col2='b' and a.col3='e' ) sq3
where sq1.col1=sq2.col1 and sq1.col2=sq2.col2
and sq1.col1=sq3.col1 and sq1.col2=sq3.col2