求一Sql语句,由列到行转换的问题

xupc 2003-07-02 11:51:31
表结构
有两列
Col1 Col2
数据
a 11
a 12
b 23
b 67
我的里面的数据都是上面这样Col1有两个重复的,
现在我想这样取数据出来
Col1, col2 as c1,col2 as c2
即将两行数据并成一行,
取出来的数据像是
a 11 12
b 23 67
请问SQl语句怎样写?
...全文
34 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xupc 2003-07-02
  • 打赏
  • 举报
回复
我不是要转换值,我是想将行转换为两列
你我按正常顺序取出来为
a 11
a 12
b 23
b 67

而我想用一条Sql语句使取出来的数据成这样
a 11 12
b 23 67

bzszp 2003-07-02
  • 打赏
  • 举报
回复
decode()
xupc 2003-07-02
  • 打赏
  • 举报
回复
我试过 bobfang(匆匆过客)的了,确实可行,多谢!
darkhorsedba 2003-07-02
  • 打赏
  • 举报
回复
to bobfang(匆匆过客) :
你试过了吗?我好长时间没写SQL了。看来无论哪件事总有最好的方法!
bobfang 2003-07-02
  • 打赏
  • 举报
回复
select col1,min(col2),max(col2) from A group by col1;
xupc 2003-07-02
  • 打赏
  • 举报
回复
谢谢各位,我的是这样解决的:
上面的表
select ANo,BNo from (
select rownum as Rn,Tb.Col2 as ANo,Ta.Col2 as BNo
from Table Tb left join Table Ta
on Ta.Col1=Tb.Col1 where Ta.id is not null and Tb.Col1 <> Ta.Col2 order by Ta.Col1
) where mod(Rn,2)=1
darkhorsedba 2003-07-02
  • 打赏
  • 举报
回复
bzszp(SongZip)的感觉简洁!
darkhorsedba 2003-07-02
  • 打赏
  • 举报
回复
你还在线吗?我在公司,机器上没有装Oracle。我有一个想法啊,但没有办法试!你能配合一下吗?
第一,你先从你表中选取ROWNUM,告诉我ROWNUM是从0开始的连续的数,还是从1开始的连续的数?
SQL> select rownum from table_name;


第二,如果是从1开始连续的值,请在where 的后面加上条件:
SQL>select rownum from table_name
where rownum=2*rownum-1 ;
如果是从0开始的连续的值,请在where的后面加上条件:
SQL>select rownum from table_name
where rownum=2*(rownum+1)-1

tell me the result !!!




beckhambobo 2003-07-02
  • 打赏
  • 举报
回复
select col1,max(decode(rm,1,col2)) c1,max(decode(rm,2,col2)) c2 from
(select col1,col2,row_number() over(partition by col1 order by rownum) rm from table_name)
where rm<=2
group by col1

以上只是只取前两行数据,至于取多行数据由用户自定义了
bzszp 2003-07-02
  • 打赏
  • 举报
回复
select t.col1,sum(decode(t.id,2,col2,0)),sum(decode(t.id,1,col2,0))
from (select mod(rownum,2)+1 id,col1,col2 from tbname) t
group by t.col1;
xupc 2003-07-02
  • 打赏
  • 举报
回复
每一个只有两列
bzszp 2003-07-02
  • 打赏
  • 举报
回复
每一个最多有多少列有限制吗?a 11 12 ...
比如:条数<20条

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧