select top 1 xx from xx
group by xx
order by xx desc

解决方案 »

  1.   

    我的表中除了表示序号的id是数字以外,其他都不是
    我用了order by id desc 还是不行的。另:看不懂楼上大侠的“top 1”喔附:我的查询语句
    insert into filteredtraps(version,enterprise,agent_ip,time,timeStamp,generic_type,bindings,specific_type,comunity,counts) 
    select version,enterprise,agent_ip,time,timeStamp,generic_type,bindings,specific_type,comunity,count(agent_ip) from trap_info 
    group by generic_type 
    order by id desc
      

  2.   

    insert into filteredtraps(version,enterprise,agent_ip,time,timeStamp,generic_type,bindings,specific_type,comunity,counts) 
    select top 1  --你不是说只要满足条件的最后一条吗? top 1就是只取一条记录
    version,enterprise,agent_ip,time,timeStamp,generic_type,bindings,specific_type,comunity,count(agent_ip) from trap_info 
    group by generic_type 
    order by generic_type
      

  3.   

    top 1 是sql server的特色吗?
    我们数据库书上并没有提到过
      

  4.   

    我觉得用order by 是不行的。
    group by 列名1 子句是按列名1进行分组,得到结果表。
    order by 列名2 子句是结果表按列名2进行排序。
                         ~~~
    也就是说group by以后已经是仅取了group中满足条件的第一行的值。
    我把selcect子句中的time改为,max(time),得到想要的结果了。
      

  5.   

    那就是time是你希望的排序字段啊
    只要order by time desc就可以了啊