有张学生表 select * from student           id      sname    ssex     birthday      class 110 游龙 男 2009-6-25      95031
108 曾华 男 1977-9-1      95033
105 匡明 男 1975-10-2      95031
107 王丽 女 1976-1-23      95033
101 李军 男 1976-2-20      95033
109 王芳 女 1975-2-10      95031
103 陆君 男 1974-6-3      95031
 
 现在想以class中的'95031'与'95033'为班级列,改造成行转列的显示
 SQL:select decode(class,'95031',sname) as T95031, decode(class,'95033',sname) as T95033 from student
    T95031 T95033
游龙
曾华
匡明
王丽
李军
王芳
陆君      问题一:我AS ‘95031’的时候说找不到From关键字,其实我就是想以数字显示列头,难道不行吗?
     问题一:怎么解决转出来的表中 空白的地方?我想转成下面这种形式
       
         T95031 T95033
游龙 曾华
匡明 王丽
王芳 李军
陆君      还弱弱的问下 能不能做成这种效果?

解决方案 »

  1.   

    靠  T95031 T95033 
    游龙 
    曾华 
    匡明 
    王丽 
    李军 
    王芳 
    陆君 我明明是这样发的
         T95031 T95033
    1 游龙
    2 曾华
    3 匡明
    4 王丽
    5 李军
    6 王芳
    7 陆君
      

  2.   

        T95031 T95033
    1 游龙 ''
    2 ‘’ 曾华
    3 匡明 ''
    4 '' 王丽
    5 '' 李军
    6 王芳 ''
    7 陆君 '' CSDN的编辑器是这么回事啊??? 不保留空白的吗? 靠
      

  3.   

     我的意思就是 怎么把 ‘’= 空白(null) 去掉呢?
     像这样:T95031 T95033 
    游龙 曾华 
    匡明 王丽 
    王芳 李军 
    陆君 
      

  4.   

    declare
      v_a test_a.sname%type;
      v_b test_a.sname%type;
      cursor curl is select sname from test_a;begin
    create view test_a_view as select sname "95031",sname "95033" from test_a where 1=2;
    open curl;loop
      
      fetch curl into v_a;
      exit when curl%notfound;
      fetch curl into v_b;
      if curl%notfound then v_b:=null;
      insert into test_a_view values(v_a,v_b);
    end loop;
    close curl;
    end;
        
         
    我写的大致这样,还有点错误
    数字作为字段名加双引号就行了
      

  5.   

     楼上的兄弟 我不是你这个意思
     你又写过程 又建视图的干什么? 
     我的意思是 只要写个查询语句就到达我要的那种效果。
     我问的是 能否把 ‘’ =(空白 or Null)去除
      

  6.   

    我看错了
    不好意思
    数据库各项值之间联系是很紧密的
    要去掉null要往上缩进的话,打破了这种联系
    每行行间的数值到底有什么关系呢?这不符合数据库的定义
    所以很有难度
      

  7.   

    我想到了,这个题可以用两个游标实现
    思路和我上面答的差不多,不过得建两个游标
    单用查询没法实现,得建个新表
    对两个新字段建2个查询,把空值去掉,游标分别插入
    再用insert into 将2个游标取值放一行插入到新表里
    两个游标一个取完的时候,就只插入另一个游标的值
    两个都取完则结束循环,用if then else就可以了
      

  8.   

    哈哈,终于可以上网了
    周末我在家研究了一下,代码写出来了,不过得建个新表
    结果如下
     create table test_a(id number primary key,sname varchar2(20),sex varchar2(10),birthday date,class number);
     insert into test_a values(110,'游龙','男',date'2009-6-25',95031);
     insert into test_a values(108,'曾华','男',date'1977-9-1',95033);
     insert into test_a values(105,'匡明','男',date'1975-10-2',95031);
     insert into test_a values(107,'王丽','女',date'1976-1-23',95033);
     insert into test_a values(101,'李军','男',date'1976-2-20',95033);
     insert into test_a values(109,'王芳','女',date'1975-2-10',95031);
     insert into test_a values(103,'陆军','男',date'1974-6-3',95031);create table test_b ("95031" varchar2(20),"95033" varchar2(20))declare
      v_a varchar2(20);
      v_b varchar2(20);
      cursor curl1 is select sname from test_a where class=95031;
      cursor curl2 is select sname from test_a where class=95033;begin
    open curl1;
    open curl2;loop  
      fetch curl1 into v_a;  
      fetch curl2 into v_b;
      exit when curl1%notfound and curl2%notfound;
      if curl1%notfound then v_a:=null;
        else if curl2%notfound then v_b:=null;
        end if;
      end if;     
      insert into test_b values(v_a,v_b); 
    end loop;
    close curl1;
    close curl2;
    end;95031 95033
    游龙 曾华
    匡明 王丽
    王芳 李军
    陆军
      

  9.   

     感谢wildwave这位兄弟 分就给你了(牵强的答案) 哈哈
     刚开始我没想到要建表,定义游标什么的。
     就是很单纯的想法,写个查询语句看能不能搞定。
     后来想下 确实有点不可能。 3Q!!!
     
      

  10.   

    重新翻了下以前的帖子
    那时初学oracle,很多都不懂
    重新写下这个代码,只是不知道你还能不能看到
    嘿嘿
    select max(case when class=95031 then sname end) "95031",
      max(case when class=95033 then sname end) "95033"
      from (
      select sname,class,row_number()over(partition by class order by rownum)rn from test_n)
      group by rn
      order by rn95031 95033
    游龙 曾华
    匡明 王丽
    王芳 李军
    陆军