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 8158059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:29:42+00:00 2026-06-06T17:29:42+00:00

relationID sessionID_Ref userID_Ref 1 1 1 2 1 2 3 2 1 4 2

  • 0
relationID     sessionID_Ref     userID_Ref
1              1                 1
2              1                 2
3              2                 1
4              2                 3
5              3                 1
6              3                 2
7              3                 3

Okey! I’m building a messaging system with the possibility to send messages to a group of people. But I’m stuck with this SQL query where to find the sessionID depending on what users I send the message to.

For example: If I (userID: 1) send a message to userID 2, the SQL Query should return sessionID: 1

If I send a message to userID 2 and 3, it sould return: sessionID: 3

Can I do this with a single sql query, using MSSQL?

  • 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-06T17:29:44+00:00Added an answer on June 6, 2026 at 5:29 pm

    Possibly something like this:

    select sessionID_Ref
    from tablename
    group by sessionID_Ref
    having count(distinct userID_Ref) = 2
       and min(userID_Ref) = 1
       and max(userID_Ref) = 2
    

    Here’s a full example:

    create table #tablename (
        relationID int,
        sessionID_Ref int,
        userID_Ref int
    )
    
    insert into #tablename values(1,1,1)
    insert into #tablename values(2,1,2)
    insert into #tablename values(3,2,1)
    insert into #tablename values(4,2,3)
    insert into #tablename values(5,3,1)
    insert into #tablename values(6,3,2)
    insert into #tablename values(7,3,3)
    
    
    create table #users (
        users int
    )
    
    insert into #users values(1)
    insert into #users values(3)
    
    
    select t.sessionID_Ref from #tablename t
    inner join #users u on t.userID_Ref = u.users
    inner join (
        select t.sessionID_Ref
        from #tablename t
        group by t.sessionID_Ref
        having COUNT(t.userID_Ref) = (select COUNT(*) from #users)
    ) aux on aux.sessionID_Ref = t.sessionID_Ref
    group by t.sessionID_Ref
    having COUNT(t.userID_Ref) = (select COUNT(*) from #users)
    
    
    drop table #tablename
    drop table #users
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is in relation to another question I have asked, but what are
In relation to this stackoverflow question , how would I go about creating my
In relation to this question ( Efficient hashCode() implementation ) I have one more
Context I'm building a persistence layer to abstract different types of databases that I'll
My database structure is something like this (I'm using declarative style): class Character(Base): __tablename__=characters
i have a query that goes like this: $get_feed_amount = mysql_query( SELECT COUNT(*) as
I'm trying to use beaker cache with SQLAlchemy but I've been receiving errors. Here
While building an ecommerce platform I have run into design problems. I'm working with
I have several classes in an application that I am currently building, and I
Hi folks would really appreciate your help in answering this rather involved question. Please

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.