2011-10-19 126 views
2

我有「GetConversationPaged」sp,它從表中選擇分頁數據,名爲「user_messages_index」,並在來自表的對話中包含「lastmesage」,名爲「social_user_messages」。 我不確定它是否可以很好地調整,但它有一個缺少的東西。我想通過lastMessageId對「Row_number」進行排序,我使用select MAX(messageId)獲得該值。SQL Server Row_number()OVER與GROUP BY

PS:我有消息索引和消息表,因爲我想讓人們發送相同的消息給多個收件人。而不是一遍又一遍地插入相同的消息我使用索引表

那麼,我怎樣才能通過最新的消息ID排序結果?


下面是桌子和SPS我使用

--index table--

CREATE TABLE [dbo].[social_user_messages_index](
[senderId] [int] NOT NULL, 
[recipientId] [int] NOT NULL, 
[messageId] [int] NOT NULL, 
[isRead] [bit] NOT NULL 
) ON [PRIMARY] 

--messages table--

CREATE TABLE [dbo].[social_user_messages](
[id] [int] IDENTITY(1,1) NOT NULL, 
[message] [nvarchar](1000) NOT NULL, 
[sendDate] [datetime] NOT NULL, 
) ON [PRIMARY] 

--sp -

ALTER PROCEDURE [dbo].[GetConversationPaged] 
@userId int, 
@isRead bit, 
@PageNumber int, 
@PageSize int 
AS 
BEGIN 

    DECLARE @FirstRow INT, @LastRow INT, @RowCount INT, @PageCount INT 
    --- find recordcount and pages 
    SELECT @RowCount = COUNT(1) OVER(), 
      @PageCount = COUNT(*) OVER()/@PageSize 
      FROM social_user_mesages_index s 
      WHERE s.recipientId = @userId and [email protected] 
      GROUP BY senderId 
    --- calculate pages 
    IF @RowCount % @PageSize != 0 SET @PageCount = @PageCount + 1 
    IF @PageNumber < 1 SET @PageNumber = 1 
    IF @PageNumber > @PageCount SET @PageNumber = @PageCount 
    --- select paging data 
    SELECT currentpage = @PageNumber, totalpages = @PageCount, totalrows = @RowCount 
    SET @FirstRow = (@PageNumber - 1) * @PageSize + 1; 
    SET @LastRow = (@PageNumber - 1) * @PageSize + @PageSize; 

    --- select records  
    WITH mytable 
     AS (

      SELECT Row_number() OVER (ORDER BY (SELECT 1)) AS rownumber, 
        (Select name from domains_users d where d.id=s.senderId) as senderName, 
        (select MAX(messageId)) as lastMessageId, 
        (select m.[message] from social_user_messages m where m.id = (select MAX(messageId))) as [message] 
        --,(select m.sendDate from social_user_messages m where m.id = (select MAX(messageId))) as lastMessageDate 
        ,senderId 
      FROM social_user_mesages_index s 
      WHERE s.recipientId = @userId and [email protected] 
      GROUP BY senderId 

     ) 

    SELECT * 
    FROM mytable 
    WHERE rownumber BETWEEN @FirstRow AND @LastRow 
    ORDER BY rownumber ASC; 
END 
+0

好, 「ROW_NUMBER()OVER(ORDER BY MAX(MESSAGEID)DESC)AS ROWNUMBER」 似乎幫助。但它對我的大腦變得複雜:)任何人都可以證實它? – nLL

回答

3

在您的CTE中,您有social_user_messages_index拼寫錯誤。我也刪除了你有幾個地方的Group BY SenderId,並更新了CTE。如果這不會做你希望的東西,讓我知道,我可以調整它:

ALTER PROCEDURE [dbo].[GetConversationPaged] 
@userId int, 
@isRead bit, 
@PageNumber int, 
@PageSize int 
AS 
BEGIN 

    DECLARE @FirstRow INT, @LastRow INT, @RowCount INT, @PageCount INT 
    --- find recordcount and pages 
    SELECT @RowCount = COUNT(1) OVER() 
     , @PageCount = COUNT(*) OVER()/@PageSize 
    FROM dbo.social_user_messages_index AS s 
    WHERE s.recipientId = @userId and [email protected] 

    --- calculate pages 
    IF @RowCount % @PageSize != 0 SET @PageCount = @PageCount + 1 
    IF @PageNumber < 1 SET @PageNumber = 1 
    IF @PageNumber > @PageCount SET @PageNumber = @PageCount 

    --- select paging data 
    SELECT currentpage = @PageNumber 
     , totalpages = @PageCount 
     , totalrows = @RowCount; 
    SET @FirstRow = (@PageNumber - 1) * @PageSize + 1; 
    SET @LastRow = (@PageNumber - 1) * @PageSize + @PageSize; 

    --- select records  
    WITH mytable AS 
    (
     SELECT Row_number() OVER (ORDER BY MessageId DESC) AS rownumber 
     , du.name as senderName 
     , m.Message 
     , senderId 
     FROM social_user_messages_index AS s 
     INNER JOIN dbo.domains_users AS du ON s.SenderId = du.id 
     INNER JOIN dbo.social_user_messages AS m ON s.messageId = m.id 
     WHERE s.recipientId = @userId 
     AND [email protected] 
    ) 
    SELECT * 
    FROM mytable 
    WHERE rownumber BETWEEN @FirstRow AND @LastRow 
    ORDER BY rownumber ASC; 
END 
+0

我也假定'dbo.domains_users'只是'id INT'和'name NVARCHAR',因爲你沒有提供更多關於它的信息。 –