首先,祝大家中秋节快乐!    我有一个表 MsgList:包含列 From_name , send_time 等。这是一个存储消息通知的表    我想让后台能 在列出数据时 把  From_name  进行group by 。同时能根据 send_time 进行排序,这样就可以让后台的管理员 看到的数据为 每个会员仅一条记录 且这些记录能按照时间排序。

解决方案 »

  1.   

    看到的数据为 每个会员仅一条记录 且这些记录能按照时间排序。
    每个会员会有多条记录,楼主想要send_time最大或者最小的记录吗?
      

  2.   

    要的,嘿嘿比如 数据From_name  send_time  send_txtabc001 2011/1/1 我发送的第一条消息
    abc002 2011/1/1 我发送的第一条消息
    abc001 2011/1/1 我发送的第2条消息
    abc001 2011/1/1 我发送的第3条消息
    abc002 2011/1/2 我发送的第2条消息
    想要得出的结果:From_name  send_time  send_txtabc002 2011/1/2 我发送的第2条消息
    abc001 2011/1/1 我发送的第3条消息
      

  3.   

    create table tb
    (From_name nvarchar(10),send_time datetime)insert into tb 
    select '张三' ,'2011-09-11' union all
    select '张三' ,'2011-09-12' union all
    select '张三' ,'2011-09-13' union all
    select '李四' ,'2011-09-10' union all
    select '李四' ,'2011-09-14' union all
    select '王五' ,'2011-09-01' 
    ---获取最小时间
    ;with cte as (
    select *,rowNum=ROW_NUMBER() over(partition by From_name order by send_time) from tb
    )select * from cte where rowNum=1---获取最大时间
    ;with cte as (
    select *,rowNum=ROW_NUMBER() over(partition by From_name order by send_time desc) from tb
    )select * from cte where rowNum=1
      

  4.   


    select * from tb a where not exists(
    select 1 from tb where From_name=a.From_name and send_time>a.send_time
    )