1

我有一个如下表

ID  |FromId |ToId   |Message|DateTime
------------------------------------------
1   |1      |2      |a      |15:00
2   |1      |2      |b      |15:01
3   |1      |2      |c      |15:02
4   |2      |1      |d      |15:03
5   |3      |1      |e      |15:04
6   |3      |1      |f      |15:05
7   |1      |3      |g      |15:06

我想要得到的是 Peers 的每一条消息。

例如:用户 1 和用户 2 有 4 条消息 (ID:1,2,3,4) 用户 1 和用户 3 有 3 条消息 (ID:5,6,7)

我只想从用户那里获取最新的消息记录,我需要一个 SQL 查询,它会给出如下结果:

*sql code here ? -- I need this.

结果(对于:其中 UserID=1):

ID  |FromId |ToId   |Message|DateTime
------------------------------------------
4   |2      |1      |d      |15:03
7   |1      |3      |g      |15:06

有任何想法吗 ?我已经尝试过 Distinct 等,但它并没有以某种方式起作用。请帮忙。

对不起,伙计们,我想我忘了提到我需要来自 Peer 的最新记录,而不是来自某个用户的最新记录,例如对于用户 1 和用户 2,我需要他们的最新记录,不知道哪个来自或哪个来自是 To.. 我需要两者的最新记录,在我们的例子中是 ID 4,没有其他记录。

4

6 回答 6

6

如果 Sql Server 2005+,您可以使用row_number() over ( ... )对记录进行分组、排序和编号,然后仅检索在其组中排在第一位的记录:

; with cte as
(
  select *,
      -- Group by user not being searched for
         row_number() over (partition by case when FromID = @UserID
                                              then ToID
                                              else FromID
                                          end
      -- Last date will be numbered as 1
                            order by [DateTime] desc
                           ) rn
    from Table1
-- Filter only messages sent from or received by certain user
   where (FromID = @UserID or ToID = @UserID)
)
select *
  from cte
-- Get last dates only
 where rn = 1
于 2012-09-11T11:39:07.267 回答
2

为此使用此 Sql:

Declare @tempTeable Table
(
 Id int,
 FromID int,
 ToId int,
 SMessage nvarchar(250),
 SDateTime Time 
)

Insert into @tempTeable values(1,1,2,'a','15:00')
Insert into @tempTeable values(2,1,2,'b','15:01')
Insert into @tempTeable values(3,1,2,'c','15:02')
Insert into @tempTeable values(4,2,1,'d','15:03')
Insert into @tempTeable values(5,3,1,'e','15:04')
Insert into @tempTeable values(6,3,1,'f','15:05')
Insert into @tempTeable values(7,1,3,'g','15:06')

select distinct t1.* from @tempTeable as t1
inner join 
  (select UserID,MAX(SDateTime)as SDateTime from
    (
      select FromId as UserId ,MAX(SDateTime)as SDateTime from  @tempTeable group by 
       FromId
       UNION
       select ToId as UserId,MAX(SDateTime)as SDateTime from  @tempTeable group by   
        ToId) as tbl
        group by UserId) as tblres 
  on (t1.FromID =tblres.UserId  or t1.toId =tblres.UserId) 
  and t1.SDateTime=tblres.SDateTime 
于 2012-09-11T11:29:09.600 回答
0
create table Msgs (ID int primary key, FromId int, ToId int, Message nvarchar(MAX), DateTime time);
insert into Msgs (ID, FromId, ToId, Message, Datetime) values (1   ,1      ,2      ,'a'      ,'15:00');
insert into Msgs (ID, FromId, ToId, Message, Datetime) values (2   ,1      ,2      ,'b'      ,'15:01');
insert into Msgs (ID, FromId, ToId, Message, Datetime) values (3   ,1      ,2      ,'c'      ,'15:02');
insert into Msgs (ID, FromId, ToId, Message, Datetime) values (4   ,2      ,1      ,'d'      ,'15:03');
insert into Msgs (ID, FromId, ToId, Message, Datetime) values (5   ,3      ,1      ,'e'      ,'15:04');
insert into Msgs (ID, FromId, ToId, Message, Datetime) values (6   ,3      ,1      ,'f'      ,'15:05');
insert into Msgs (ID, FromId, ToId, Message, Datetime) values (7   ,1      ,3      ,'g'      ,'15:06');
select * from Msgs;

with M(ID, FromId, ToId, Message, DateTime, RN) as (
select ID, FromId, ToId, Message, DateTime,
    ROW_NUMBER() over (partition by 
        CASE WHEN FromId < ToID THEN FromId ELSE ToID END, 
        CASE WHEN FromId > ToID THEN FromId ELSE ToID END 
        order by DateTime desc)
from Msgs)
select ID, FromId, ToId, Message, DateTime from M where RN = 1;

drop table Msgs;

退货

ID  FromId  ToId    Message DateTime
1   1   2   a   15:00:00.0000000
2   1   2   b   15:01:00.0000000
3   1   2   c   15:02:00.0000000
4   2   1   d   15:03:00.0000000
5   3   1   e   15:04:00.0000000
6   3   1   f   15:05:00.0000000
7   1   3   g   15:06:00.0000000

ID  FromId  ToId    Message DateTime
4   2   1   d   15:03:00.0000000
7   1   3   g   15:06:00.0000000
于 2012-09-11T11:43:04.643 回答
0

您需要进行自引用连接,因此内部查询选择每个组的最大值,外部查询选择匹配的每一行的所有数据。

select * 
from thetable t1 
join(select max(ID) as id,
            FromID 
     from thetable
     group by FromID)t2 on (t1.id=t2.id);
于 2012-09-11T11:12:09.867 回答
0

尝试这个:

;WITH CTE As(
select id,fromID,ToID,Message,DateTime,0 as sel from Msgs where id=1
union all
select m.id,m.fromID,m.ToID,m.Message,m.DateTime,
CASE WHEN (m.FromId =c.FromId or m.FromId =c.ToId) then 0 else 1 end as sel 
from CTE c inner join Msgs m 
--on (c.FromId = m.FromId and c.ToId = m.ToId) or (c.FromId = m.ToId and c.ToId = m.FromId)
on m.id=c.Id+1
)

select * from CTE where ID in (select ID-1 from CTE where sel=1)
union
select * from CTE where ID = (select max(id) from CTE)
于 2012-09-11T12:08:48.687 回答
-1
create function getlastmessage(
@userid int
)
returns nvarchar(max)
as
select top(1), message from messages 
 where userid=@userid order by messageid desc

在人员表中

select *, getlastmessage(actorid) as lastmessage from people

有关函数语法的更多信息


如果你不想使用函数

select 
*, 
(select top(1), message from messages 
  where messages.userid=people.userid order by messageid desc) as lastmessage 
 from people

会有点乱。

于 2012-09-11T11:11:35.180 回答