这个问题好像看起来不难,以前也做过,但是现在想想好像挺难出来的
比如:
  某个表有三个字段,我要取出 同个id的时间最大的那条记录
 ID    时间      金额
 1    10:00     10
 2    08:50     20
 1    09:50     200
 2    11:00     5我想取到的数据是:
 ID      时间      金额
 1       10:00    10
 2       11:00     5怎么取呢

解决方案 »

  1.   

    select id,时间,金额 from
    (
      select id,时间,金额,dense_rank()over(partition by id order by 时间 desc) rn from 表1
    )
    where rn=1
      

  2.   

    你也可以使用max(时间),查询起来一样
      

  3.   

    select id, max(时间),金额 from table group by id 
    是不是应该去掉金额 或者改为sum(金额) 才正确呢
      

  4.   

    没什么技术的SQL语句,一楼的比较有技术含量
    select * from 表1 b,
    (select id ,max(时间) 时间 from 表1 group by id) a
    where a.id=b.id and a.时间.时间
      

  5.   

    没什么技术的SQL语句,一楼的比较有技术含量
    select * from 表1 b,
    (select id ,max(时间) 时间 from 表1 group by id) a
    where a.id=b.id and a.时间=b.时间