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

  • Home
  • SEARCH
  • 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 8366719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:59:08+00:00 2026-06-09T12:59:08+00:00

I’ve inherited a system using SQL Server (2008 R2) with two tables that cover

  • 0

I’ve inherited a system using SQL Server (2008 R2) with two tables that cover system messaging. A message has a one to many relationship, it can be sent to multiple users.

Primary table, Messaging, looks like:

Id (PK, auto increment)
Subject
MessageBody
CreatorEmployeeId
ConversationId
IsConversationStarter (bool)

Secondary table, MessagingDetails, looks like:

ID (PK, auto increment)
MessageId (FK to Messaging PK)
RecipientEmployeeId
ConversationId
IsRead (bool)

If that’s not clear, a new message is put into the Messaging table with an employee Id for who created the message, and new Conversation Id is created. Then X number of rows will be inserted into MessagingDetails, one per recipient, with references back to the Messaging table and using the same Conversation Id.

Any new replies will look the same, reply message goes into the Messaging table, recipient details go into MessagingDetails using the same Conversation Id, so that you can pull a whole message thread by one number.

This gets sloppy when you’re trying to get a count of all unread messages that you’ve created or that have been sent to you. Here’s an existing query that gets all of the unread messages sent to the logged-in user by other users in the system, where they created the message originally:

SELECT * FROM dbo.Messaging m
INNER JOIN dbo.MessagingDetails m1 on m.Id = m1.MessageId
INNER JOIN dbo.MessagingDetails m2 on m1.ConversationId = m2.ConversationId
WHERE m1.RecipientEmployeeId = @employeeId
AND m.IsConversationStarter = 1
AND m.ConversationId = m1.ConversationId
AND m2.IsRead = 0 
AND m2.RecipientEmployeeId == @employeeId

Then to find all messages that the employee created but have unread replies, you have to do an almost identical query, swapping out the first part of the WHERE clause for:

m.CreatorEmployeeId = @employeeId

I loathe the inner joining the same table twice, once to itself. Given that schema outlined above, is there a better way to write these queries?

  • 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-06-09T12:59:09+00:00Added an answer on June 9, 2026 at 12:59 pm

    For “messages send by employee that have unsent replies”, I think this solves the problem:

    with EmployeeCreatedMessages as (
         select distinct MessageId
         from Messaging
         where m.CreatorEmployeeId = @employeeId
        )
    select distinct MessageId
    from MessageDetails md
    where md.MessageId in (select MessageId from EmployeeCreatedMessages) and
          md.IsRead = false
    

    I’m not sure what information you want about the message. This just gives the message id.

    The same approach works for all messages sent to users by others:

    with OtherCreatedMessages as (
         select distinct MessageId
         from Messaging
         where m.CreatorEmployeeId <> @employeeId
        )
    select distinct MessageId
    from MessageDetails md
    where md.MessageId in (select MessageId from OtherCreatedMessages) and
          md.IsRead = false and
          md.RecipientEmployeeId = @employeeid
    

    Your description and your question don’t mention conversionid. Is this relevant?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.