select * from (
select pname,p1 P from celltable union all
select pname,p2 P from celltable union all
select pname,p3 P from celltable union all
select pname,p4 P from celltable union all
select pname,p5 P from celltable union all
select pname,p6 P from celltable union all
select pname,p7 P from celltable union all
select pname,p8 P from celltable union all
select pname,p9 P from celltable )
where p is not null
order by pname,p

解决方案 »

  1.   


    select * from (
    select pname,p1 P from celltable union all
    select pname,p2 P from celltable union all
    select pname,p3 P from celltable union all
    select pname,p4 P from celltable union all
    select pname,p5 P from celltable union all
    select pname,p6 P from celltable union all
    select pname,p7 P from celltable union all
    select pname,p8 P from celltable union all
    select pname,p9 P from celltable )
    where p is not null
    order by pname,p有没有其他方法 精简点的 union all 效率如何?
      

  2.   

    要么Union ALL要么你程序自己做
      

  3.   


    select * from (
    select pname,p1 P from celltable union all
    select pname,p2 P from celltable union all
    select pname,p3 P from celltable union all
    select pname,p4 P from celltable union all
    select pname,p5 P from celltable union all
    select pname,p6 P from celltable union all
    select pname,p7 P from celltable union all
    select pname,p8 P from celltable union all
    select pname,p9 P from celltable )
    where p is not null
    order by pname,p版主大婶啊  排序不对哦   不是我想要的顺序
      

  4.   


    select * from (
    select pname,p1 P from celltable union all
    select pname,p2 P from celltable union all
    select pname,p3 P from celltable union all
    select pname,p4 P from celltable union all
    select pname,p5 P from celltable union all
    select pname,p6 P from celltable union all
    select pname,p7 P from celltable union all
    select pname,p8 P from celltable union all
    select pname,p9 P from celltable )
    where p is not null
    order by pname,p版主大婶啊  排序不对哦   不是我想要的顺序
    你想按什么规则排序啊,看不懂你的排序啊
      

  5.   


    select * from (
    select pname,p1 P from celltable union all
    select pname,p2 P from celltable union all
    select pname,p3 P from celltable union all
    select pname,p4 P from celltable union all
    select pname,p5 P from celltable union all
    select pname,p6 P from celltable union all
    select pname,p7 P from celltable union all
    select pname,p8 P from celltable union all
    select pname,p9 P from celltable )
    where p is not null
    order by pname,p版主大婶啊  排序不对哦   不是我想要的顺序
    你想按什么规则排序啊,看不懂你的排序啊 
    PName  P  (根据P1-P9顺序依次下来 空值去掉)
    1001   A1
    1001   A2
    1001   A3
    1001   A6
    1001   A7
    1001   A8
    1002   A1
    1002   ..
    1002   A7
    1003   A1
    1003   A5
    1003   A3

    1003   ..
    1003   A9like this
      

  6.   

    你前面都是按照a1,a2...a8,这三个又按照a1,a5,a3,请问你排序根据是什么?
      

  7.   

    你前面都是按照a1,a2...a8,这三个又按照a1,a5,a3,请问你排序根据是什么?
    根据p1 - p9 顺序来
    p1 值是 a1
    p2 值是 a9
    p3 值是 a3 显示的话就是 a1,a9,a3
      

  8.   

    你前面都是按照a1,a2...a8,这三个又按照a1,a5,a3,请问你排序根据是什么?
    麻烦版主了  
      

  9.   

    你前面都是按照a1,a2...a8,这三个又按照a1,a5,a3,请问你排序根据是什么?
    麻烦版主了  
    select *
      from (select pname, p1 P, 1 seq
              from celltable
            union all
            select pname, p2 P, 2 seq
              from celltable
            union all
            select pname, p3 P, 3 seq
              from celltable
            union all
            select pname, p4 P, 4 seq
              from celltable
            union all
            select pname, p5 P, 5 seq
              from celltable
            union all
            select pname, p6 P, 6 seq
              from celltable
            union all
            select pname, p7 P, 7 seq
              from celltable
            union all
            select pname, p8 P, 8 seq
              from celltable
            union all
            select pname, p9 P, 9 seq
              from celltable)
     where p is not null
     order by pname, seq;
      

  10.   

    pivot /unpivot
    很强大的东西,呵呵,刚去学习了下
    http://www.cnblogs.com/zlja/archive/2011/12/10/2449515.html
      

  11.   

    great,thanks for share
    这个函数在sql2008已经有了 可惜不是11G  看到oracle的语法  有时候还是觉得sql 好使