a   b   c   d   e    f    g    h   i   j  
28  89  56  43  58   32   62   65  21  32
101 72  90  21  32    4   12   34  11  201假如说现在有a到j这么多列 查询出这张表的这么多列的一个最大值
只想到了用union相关联查,看看大家有没有什么好的办法,效率高一些的

解决方案 »

  1.   


    select max(col) from 
    (select max(a) col from tb
    union
    select max(b) from tb union
    select max(c) from tb union
    select max(d) from tb union
    ...) t
      

  2.   


    我局的用这个union 效率不高吧 数量大了反应行吗
      

  3.   

    你有多少数据量啊,目前我能想到的只能union好一点,用unpivot应该也快不到哪里去吧。
      

  4.   


    create table TryTable
    (a int,b int, c int,d int,e int,f int,g int,h int,i int,j int)
    insert TryTable
    select 28  ,89  ,56  ,43  ,58   ,32   ,62   ,65  ,21 , 32 union all
    select 101 ,72  ,90  ,21  ,32    ,4   ,12   ,34  ,11 , 201select max(maxvaluse)  as maxvaluse from TryTable 
    unPivot(maxvaluse for [type] in(a,b,c,d,e,f,g,h,i,j)) as a--maxvaluse
    -------------
    --201
      

  5.   


    --create table #a(id int,a  int, b int,  c int,  d int,  e  int,  f  int,  g int,   h int,  i  int, j int )
    --insert into #a values(1,28,  89 , 56 , 43 , 58   ,32 ,  62 ,  65 , 21 , 32)
    --insert into #a values(1,101 ,72 , 90 , 21 , 32 ,   4 ,  12 ,  34 , 11 , 201)select * from #adeclare @a int
    declare @b int
    select @a=102
    select @b=1
    while @b<>0
    begin
    select
    @b=SUM(a)+SUM(b)+SUM(a)+SUM(b)+SUM(b)+SUM(e)+SUM(f)+SUM(g)+SUM(h)+SUM(i)+SUM(j)
    from
    (
    select ID,
    case when a-@a<0 then 0 else a-@a end a,
    case when b-@a<0 then 0 else b-@a end b,
    case when c-@a<0 then 0 else c-@a end c,
    case when d-@a<0 then 0 else d-@a end d,
    case when e-@a<0 then 0 else e-@a end e,
    case when f-@a<0 then 0 else f-@a end f,
    case when g-@a<0 then 0 else g-@a end g,
    case when h-@a<0 then 0 else h-@a end h,
    case when i-@a<0 then 0 else i-@a end i,
    case when j-@a<0 then 0 else j-@a end j
    from #a
     ) tt
    group by id
    select @a=@a+1
    end
    select @a-1 as maxnum
      

  6.   

    这个应该目前也只能用union了
      

  7.   

    SELECT ID 
    2 FROM 
    3 (SELECT ID 
    4 FROM T 
    5 ORDER BY ID DESC NULLS LAST)
    6 WHERE ROWNUM = 1;ID
      

  8.   

    用unPivot這類函數取最大值性能沒有union all好,樓主可以各列分別建上索引試試