select  a.personid,personname,status
from  person a ,status b,
(select personid,max(dt) dt from status group by  personid,convert(char(10),dt,120) c
where a.personid=b.personid and b.personid=c.personid and b.dt=c.dt

解决方案 »

  1.   

    因为一个人可以有好几个状态,我想得到一个列表r(personid,dt,status),里面列出每个人的所有状态,但同一天里的状态只要最晚的那个。刚才没有仔细检查,注意:一个人在不同的天里的状态都要列出来,而不只是要最后一个状态
    请大力兄再出一把力:P
    好象有点矛盾
      

  2.   

    select * from status a
    where dt=(select max(dt) from status where personid=a.personid and convert(char(10),dt,120)=convert(char(10),a.dt,120))
      

  3.   

    上面的是最后的状态
    下面是全部的
    select  a.personid,personname,status
    from  person a ,status b where a.personid=b.personid
      

  4.   

    caiyunxia(monkey),我的意思是说一个人一天里可能换好几个状态(在状态表里有好几条记录),但这些同一天里的状态记录只留最晚的一个。也就是说对于每一天,只要这个人有状态就显示1条,不多也不少,当然他也可能在这一天没有状态改变(在状态表里没有记录)
      

  5.   

    select  a.personid,personname,status
    from  person a ,status b,
    (select personid,max(dt) dt from status group by  personid,convert(char(10),dt,120) c
    where a.personid=b.personid and b.personid=c.personid and b.dt=c.dt
      

  6.   

    试下先,caiyunxia(monkey)的应该是对的
      

  7.   

    create table #person(personid int,personname varchar(100))
    insert #person values(1,'zhangsan')
    insert #person values(2,'lisi')create table #status(personid int,status int,dt datetime)
    insert #status values(1,1,'2003-5-1 18:00:00')
    insert #status values(1,2,'2003-5-1 19:00:00')
    insert #status values(1,3,'2003-5-2 10:00:00')
    insert #status values(2,1,'2003-5-1 12:00:00')select * from #status a where dt=(select max(dt) from #status where personid=a.personid group by CONVERT(varchar(10),dt,120) having CONVERT(varchar(10),dt,120)=CONVERT(varchar(10),a.dt,120))
    go
    drop table #person ,#status
      

  8.   

    caiyunxia(monkey)的第一个回复能返回我要的结果,但现在数据太少,不知道对不对,过一会给分哦:)