select stuff,convert(varchar(6),date,112) as date,num from rol a where stuff in 
(select top 10 stuff from rol where stuff=a.stuff order by num desc)

解决方案 »

  1.   

    select * from #t aa
    where (select count(*) 
          from #t 
          where stuff=aa.stuff 
                and datediff(month,date,aa.date)=0 
                and num<aa.num)<=10
      

  2.   

    select stuff,convert(varchar(6),date,112) as date,num from rol a where stuff in 
    (select top 10 stuff from rol where stuff=a.stuff order by num desc) 
    order by stuff,num desc
      

  3.   

    select * from rol aa
    where (select count(*) 
          from rol
          where stuff=aa.stuff 
                and datediff(month,date,aa.date)=0 
                and num<aa.num)<=10
    order by aa.stuff,date,num desc
      

  4.   

    晕,还有一个月的问题:
    select stuff,convert(varchar(6),date,112) as date,num from rol a where stuff in 
    (select top 10 stuff from rol where stuff=a.stuff and datediff(month,date,a.date)=0 order by num desc) 
    order by stuff,num desc
      

  5.   

    LJWS(归来) ,你的程式可以解释一下吗,就是stuff in (select top 10 stuff from rol where stuff=a.stuff这一行的,
      

  6.   

    我不知道有没有误会我的意思,我是需要取出每种物品每个时间段的num在前10的值,
      

  7.   

    这样   是前3,还是前10
    select * from rol aa
    where (select count(*) 
          from rol
          where stuff=aa.stuff 
                and datediff(month,date,aa.date)=0 
                and num>aa.num)<=10
    order by aa.stuff,date,num desc
      

  8.   

    stuff    date    num
    1 200405 1
    1 200405 2
    1 200405 3
    1 200405 4
    1 200405 5
    1 200406 6
    1 200406 7
    1 200406 8
    1 200406 9
    1 200406 10
    2 200405 11
    2 200405 12
    2 200405 13
    2 200405 14
    2 200405 15
    2 200406 16
    2 200406 17
    2 200406 18
    2 200406 19
    2 200406 20
    我做的简单点吧,就把date做成VAR的,大家可以一下子看清楚,
      

  9.   

    select * from rol aa
    where (select count(*) 
          from rol
          where stuff=aa.stuff 
                and right(date,2)=right(aa.date,2) 
                and num>aa.num)<=10
    order by aa.stuff,date,num desc
      

  10.   

    select * from rol aa
    where (select count(*) 
          from rol
          where stuff=aa.stuff 
                and right(date,2)=right(aa.date,2) 
                and num>aa.num)<=3
    order by aa.stuff,date,num desc
      

  11.   

    create proc p_t
    as
     return (select count(*) from 表)
    declare @r int 
    exec  @r=p_t
    print @r
      

  12.   

    select * from rol aa
    where (select count(*) 
          from rol
          where stuff=aa.stuff 
                and date=aa.date   --这里改了,原来的好像不太对
                and num>aa.num)<=3
    order by aa.stuff,date,num desc
      

  13.   

    再改一下,小李,好不,DATE现在是日期型的,要算每月前三,
    就是date的格式是DATETIME,2004-01-02 00:00:00.000这样的,
    我试着改,效果不好,你再改改
      

  14.   

    用这个就行了
    select * from rol aa
    where (select count(*) 
          from rol
          where stuff=aa.stuff 
                and datediff(month,date,aa.date)=0 
                and num>aa.num)<=10
    order by aa.stuff,date,num desc
      

  15.   

    select stuff,convert(varchar(6),date,112) as date,num from rol a where stuff in 
    (select top 10 stuff from rol where datediff(month,date,a.date)=0 order by num desc) 
    order by stuff,num desc