表结构和数据如下:
星期ID,星期一,星期二,星期三,星期四,星期五
     1,    1,     2,     3,     4,     5
     2,    3,     1,    10,     3,     6
     3,想找出四周内最大的三个值,sql怎么写?

解决方案 »

  1.   

    select top 3 * from
    (
      select * from 
      (
        select 星期ID , 星期一 as value
        union all
        select 星期ID , 星期二 as value
        union all
        select 星期ID , 星期三 as value
        union all
        select 星期ID , 星期四 as value
        union all
        select 星期ID , 星期五 as value
      ) t
      order by desc
    ) m
      

  2.   

    select top 3 * from
    (
      select * from 
      (
        select 星期ID , 星期一 as value
        union all
        select 星期ID , 星期二 as value
        union all
        select 星期ID , 星期三 as value
        union all
        select 星期ID , 星期四 as value
        union all
        select 星期ID , 星期五 as value
      ) t
      order by value desc
    ) m
      

  3.   

    select top 3 * from 
      (
        select 星期ID , 星期一 as value
        union all
        select 星期ID , 星期二 as value
        union all
        select 星期ID , 星期三 as value
        union all
        select 星期ID , 星期四 as value
        union all
        select 星期ID , 星期五 as value
      ) t
      order by value desc呵呵。。一层够了吧?
      

  4.   

    select top 3 * from
    (
      select * from 
      (
        select 星期ID , 星期一 as value
        union all
        select 星期ID , 星期二 as value
        union all
        select 星期ID , 星期三 as value
        union all
        select 星期ID , 星期四 as value
        union all
        select 星期ID , 星期五 as value
      ) t where BETWEEN(1 and 4)
      order by desc
    ) m
      

  5.   

    select top 3 * from 
      (
        select 星期ID , 星期一 as value from 表
        union all
        select 星期ID , 星期二 as value from 表
        union all
        select 星期ID , 星期三 as value from 表
        union all
        select 星期ID , 星期四 as value from 表
        union all
        select 星期ID , 星期五 as value from 表
      ) t
      order by value desc
      

  6.   

    set rowcount 3  select * from 
      (
        select 星期ID , 星期一 as value
        union all
        select 星期ID , 星期二 as value
        union all
        select 星期ID , 星期三 as value
        union all
        select 星期ID , 星期四 as value
        union all
        select 星期ID , 星期五 as value
      ) t
      order by value desc
    set rowcount 0
      

  7.   


    create table T(星期ID int,星期一 int, 星期二 int,星期三 int,星期四 int,星期五 int)
    insert T select  1,1,2,3,4,5
    union all select 2,3,1,10,3,6
    union all select 3,1,12,13,5,8
    union all select 4,5,4,14,3,6
    union all select 5,100,120,1,50,60select top 3 值=星期一 from 
    (
    select 星期ID, 星期一 from T 
    union all 
    select 星期ID, 星期二 from T 
    union all 
    select 星期ID, 星期三 from T 
    union all
    select 星期ID, 星期四 from T 
    union all
    select 星期ID, 星期五 from T 
    )tmp where 星期ID<5
    order by 星期一 desc--result
    值           
    ----------- 
    14
    13
    12(3 row(s) affected)
      

  8.   

    谢谢大家。
    在这里我对业务做了简化。
    实际业务是这样的:一个月一张表,每天一条记录,一条记录24列(24个小时),求一年里最大的三十个。
    用UNION的话,会报出超了最大表(256)的错。12*24 = 288
    我现在的做法是:先把数据插入临时表,然后再检索。做法和marco08(天道酬勤)得有点像。
    我想看看大家有没有更好的办法。