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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:32:34+00:00 2026-05-26T15:32:34+00:00

I have table where I have some sent sms’es by operaters and some by

  • 0

I have table where I have some sent sms’es by operaters and some by users, in both cases in column PhoneNumber is users phone

{PhoneNumber Date Type SMS Response}

Type is 1 or 2, when value is 1, message is sent by operater, when value is 2, message is sent by user. In first case, there is response filled, in second, there is SMS.

I need to show messages sent by operaters and replies by users.

If this is example (sorted by time):

user message 1
user message 2
operators message 1
operators message 2
operators message 3
users message 3
users message 4

it should be sorted like this:

operators message 1   null
operators message 2   null
operators message 3  users message 3 users message 4

(I “selected just” row with messages)

so this is my idea, to separate users and operaters message in two temp tables, and left join them on PhoneNumber = PhoneNumber, but I dont know to do those time conditions.

Basically any solutions that will list operaters messages and answer (that should be determined by times, if three operators messages in row and then users message, its answerd on third, and for first two answer should be null, and users messages before first operators message are not important).

I did my best to explain, if you have questions, please ask, and thanks in advance for any ideas.

  • 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-26T15:32:34+00:00Added an answer on May 26, 2026 at 3:32 pm

    This should work in t-SQL:

    CREATE table #msg(id integer, type integer, msg nvarchar(max), ts datetime)
    
    INSERT INTO #msg VALUES (1, 2, 'user msg 1',     '2011-11-01 16:00')
    INSERT INTO #msg VALUES (2, 2, 'user msg 2',     '2011-11-01 16:01')
    INSERT INTO #msg VALUES (3, 1, 'operator msg 1', '2011-11-01 16:02')
    INSERT INTO #msg VALUES (4, 1, 'operator msg 2', '2011-11-01 16:03')
    INSERT INTO #msg VALUES (5, 1, 'operator msg 3', '2011-11-01 16:04')
    INSERT INTO #msg VALUES (6, 2, 'user msg 3',     '2011-11-01 16:05')
    INSERT INTO #msg VALUES (7, 2, 'user msg 4',     '2011-11-01 16:06')
    
    ;WITH
       o AS (
      SELECT id, ts, msg
      FROM   #msg
      WHERE  type = 1
      )
      ,u AS (
      SELECT id, ts, msg, (SELECT TOP 1 id FROM o
                           WHERE  o.ts <= x.ts ORDER BY ts DESC) AS o_id
      FROM   #msg x
      WHERE  type = 2
      )
    SELECT o.msg, (SELECT u.msg + ',' FROM u WHERE  u.o_id = o.id
                   ORDER  BY u.ts, u.id FOR XML PATH('')) AS u_msg
    FROM   o
    ORDER  BY o.ts
    

    See a demo at data.stackexchange.com

    Major points:

    • Use CTE to separate user messages and assign the preceding operator message to each.
    • GROUP BY operator message in the final step.
    • Use FOR XML to substitute for the missing aggregate concatenation function.
      This related answer was helful: Concatenate row values T-SQL
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that saves some account limits like users. For most rows
I have table for comunication between clients and operaters, which contains phone, id (1=client,
I have table with some fields that the value will be 1 0. This
I have some table: table ASK with idask table PREFERENCES with idpref , fk_idask
I have some table structure: <tr class=row-2><tr> <tr class=row-3>..<tr> <tr class=row-4>..<tr> <tr class=row-5>..<tr> <tr
I have a table containing some names and their associated ID, along with a
I have a table that contains some blob fields that I don't want to
I have an Excel table with some data that I would like to export
I have a table that includes some student group name, lesson time, day names
When I have a table view with some sections which have their own headers

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.