Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6724981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:45:10+00:00 2026-05-26T09:45:10+00:00

I have GetConversationPaged sp that selects paged data from table called user_messages_index and includes

  • 0

I have “GetConversationPaged” sp that selects paged data from table called “user_messages_index” and includes “lastmesage” in conversation from table called “social_user_messages”.
I’m not sure if it can be fine tuned but it works with one thing missing. I want to sort “Row_number” by lastMessageId which i get with select MAX(messageId).

PS: I have message index and messages tables because i wanted people to send same message to multiple recipients. Instead of inserting same message over and over i use index table

So, how can I sort results by latest message id?


Here are table and sps i’m using

–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 s.isRead=@isRead
            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 s.isRead=@isRead
            GROUP BY senderId 

         ) 

    SELECT * 
    FROM   mytable 
    WHERE  rownumber BETWEEN @FirstRow AND @LastRow 
    ORDER  BY rownumber ASC; 
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:45:11+00:00Added an answer on May 26, 2026 at 9:45 am

    In your CTE, you had social_user_messages_index misspelled. I also removed the Group BY SenderId you had a few places, and updated the CTE. If this doesn’t do what you were hoping, let me know and I can tweak it:

    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 s.isRead=@isRead
    
       --- 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 s.isRead=@isRead
       )
       SELECT * 
       FROM   mytable 
       WHERE  rownumber BETWEEN @FirstRow AND @LastRow 
       ORDER  BY rownumber ASC;
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a photography site that I want to prevent image copying from. How can
Have a table called person. which has personid and contactid. if a person is
Have an object which is an array (not arraylist or generic) that could hold
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the
Have some dates in my local Oracle 11g database that are in this format:
Have not done this before, so obviously I suck at it. Here 64 pixels
Have not done this before ( except in java , look how Steve McLeod
Have a script to add / remove options from a select field -- the
Have an app that can use tts to read text messages. It can also
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.