部门   数据A  数据B  上传时间 
部门1   1      2      2009-01
部门1   1      2      2009-02
部门1   1      2      2009-03
部门2   1      2      2009-01
部门2   1      2      2009-02
部门2   1      2      2009-03
部门3   1      2      2009-01
部门3   1      2      2009-02
我想查询得到:
部门1   1      2      2009-03
部门1   2      2      2009-03
部门1   3      2      2009-02
也就是每个部门最后一个月上传的数据
小弟没有想到好的方法,请兄弟姐妹们帮帮,谢谢了

解决方案 »

  1.   

    试试:select distinct 部门,数据A, 数据B,上传时间 from 表 where (部门,上传时间) in
    (
      select 部门,max(上传时间) from 表 group by 部门
    )
      

  2.   

    select 部门,数据A ,数据B,max(上传时间) from table group by 部门,数据A ,数据B
    不知道可不可以
      

  3.   

    select a.*
    from 
    (select rownum rn,t.* from tb t)a,
    (select 部门,max(rn) rn
    from 
    (select rownum rn,t.* from tb t)
    group by 部门
    )b
    where a.部门=b.部门 and a.rn=b.rn
      

  4.   

    select 部门,rownum ,数据B,max(上传时间) from table group by 部门,数据B 
      

  5.   

    部门1  1      2      2009-03 
    部门2  1      2      2009-03 
    部门3  1      2      2009-02 
    看了你的需求,你应该是要这样一个结果吧?还是要用ROW_NUMBER()...select 部门,  数据A,  数据B,  上传时间 from
    (SELECT ROW_NUMBER()over(partition by 部门 order by 上传时间 desc ) as rn,a.* from yourtable a)
    where rn=1
      

  6.   

    如果是sybase数据库我应该怎么取呢?
      

  7.   

    select  bm,a,b,accept_time from lim_table_t where (bm,accept_time)in
    (select bm,max(accept_time) from lim_table_t
    group by bm
    )这个可以,已经试过了。简单易懂,谢谢
      

  8.   

    select  bm,a,b,accept_time from lim_table_t where (bm,accept_time)in 
    (select bm,max(accept_time) from lim_table_t 
    group by bm 
    ) 这个好理解些,其他都看不董