如何从查询中的排除行中选择具有关联ID的最新最新记录?

时间:2015-10-26 06:25:15

标签: sql sql-server sql-server-2008 tsql

我有一个下面的示例表和我使用的sql语句,但我需要一些关于此问题的帮助。 基于下表我只需要根据与UseEmail关联的MessageID获取最新的最新结果 被排除的行。通过使用下面的语句sql,它将给出与之关联的一组记录的所有结果 MessageID但我需要的是基于在ID顺序中按Id排序的MessageID的最新结果。有关进一步说明,请查看以下所有详细信息。谢谢!

到目前为止使用的Sql语句,

select  Id,UserEmail,SellerEmail,Messages,UserName,MessageID 
from [MessagesTab] 
where MessageID in
( 
    select  MessageID 
    from [MessagesTab]
    where UserEmail = @useremail
) 
and UserEmail <> @useremail  Order by Id Desc;

表格

 Id      UserEmail                SellerEmail         Messages             UserName       MessageID
 19     ddfgdfjn84@outlook.com    KTV21@outlook.com   Hi!...               Katie          13d6c0c9
 20     KTV21@outlook.com         KTV21@outlook.com   Hello.               Seller's Name  13d6c0c9  
 21     ddfgdfjn84@outlook.com    KTV21@outlook.com   Where are you?       Katie          13d6c0c9  
22     dsffhfg56@gmail.com        KTV21@outlook.com   When will you call?  Jenny          69e37491  
23     KTV21@outlook.com          KTV21@outlook.com   I'll meet you there. Seller's Name  13d6c0c9  
24     KTV21@outlook.com          KTV21@outlook.com    Ok. Let's go.        Seller's Name  69e37491

@useremail = ddfgdfjn84@outlook.com - 会产生结果

23     KTV21@outlook.com    KTV21@outlook.com      I'll meet you there.    Seller's Name      13d6c0c9
20     KTV21@outlook.com    KTV21@outlook.com      Hello.                  Seller's Name      13d6c0c9

但我只需要基于消息ID 的最新最新结果,标识 desc 顺序排序,如下所示

23     KTV21@outlook.com    KTV21@outlook.com      I'll meet you there.    Seller's Name      13d6c0c9

@useremail = KTV21@outlook.com 相同 - 它会给出结果

22      dsffhfg56@gmail.com     KTV21@outlook.com     When will you call?   Jenny       69e37491
21      ddfgdfjn84@outlook.com  KTV21@outlook.com     Where are you?        Katie       13d6c0c9
19      ddfgdfjn84@outlook.com  KTV21@outlook.com     Hi!...                Katie       13d6c0c9

但我只需要基于消息ID 的最新最新结果,标识 desc 顺序排序,如下所示

22         dsffhfg56@gmail.com   KTV21@outlook.com   When will you call?    Jenny       69e37491
21      ddfgdfjn84@outlook.com   KTV21@outlook.com   Where are you?         Katie       13d6c0c9

2 个答案:

答案 0 :(得分:2)

    select * from (
select  Id,UserEmail,SellerEmail,Messages,UserName,MessageID ,
row_number () over (partition by UserEmail order by ID desc  ) as rnm
from [MessagesTab] 
where MessageID in
( 
    select  MessageID 
    from [MessagesTab]
    where UserEmail = 'ddfgdfjn84@outlook.com'
) 
and UserEmail <> 'ddfgdfjn84@outlook.com' ) x
where rnm =1;

答案 1 :(得分:2)

使用ROW_NUMBER() OVER(PARTITION BY MessageID ORDER BY ID DESC)可以解决问题:

SELECT 
    Id, UserEmail, SellerEmail, Messages, MessageID
FROM (
    SELECT *,
        RN = ROW_NUMBER() OVER(PARTITION BY MessageID ORDER BY ID DESC)
    FROM MessagesTab
    WHERE 
        UserEmail <> @useremail
        AND MessageID IN(
            SELECT MessageID
            FROM MessagesTab
            WHERE UserEmail = @useremail
        )
) t
WHERE RN = 1
ORDER BY ID DESC