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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:48:34+00:00 2026-06-12T04:48:34+00:00

I have a database table looks like this: EVENT_ID TEXT_FRO TEXT_TO 55001 05 55001

  • 0

I have a database table looks like this:

EVENT_ID TEXT_FRO TEXT_TO
55001              05
55001    05        10
55001    10        15
55001    15        20
55001    20        30
56215    06        11
56215    11        22

I need to write a query (or a SP) to produce a result set to list all movements for each distinct event_ID which looks like this:

Event ID Movements
55001    05 10 15 20 30
56215    06 11 22

How can I do that?

*Edit to simplify the example

  • 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-12T04:48:36+00:00Added an answer on June 12, 2026 at 4:48 am

    It is pretty straight forward if you use a function and a cursor:

    use tempdb
    go
    
    create table tmp (
      EVENT_ID int, 
      TEXT_FRO varchar(10), 
      TEXT_TO varchar(10)
    )
    go
    
    insert into tmp values
    (55001, NULL, '05'),
    (55001, '05', '26'),
    (55001, '26', '28'),
    (55001, '28', '27'),
    (55001, '27', '26'),
    (55001, '26', '27'),
    (55001, '27', '28'),
    (55001, '28', '30'),
    (55001, '30', '40'),
    (56215, '06', '11'),
    (56215, '11', '22')
    go
    

    You have to create a function to assemble the concatenated string:

    create function fnConcat (@id int) returns varchar(255) as
    begin
      declare @rtn varchar(255) = '', @fro varchar(10), @to varchar(10), @cnt int = 1
    
      declare cr cursor local for
        select TEXT_FRO, TEXT_TO
        from   tmp
        where  EVENT_ID = @id
    
      open cr
      fetch next from cr into @fro, @to
    
      while @@fetch_status = 0
      begin
        if @cnt = 1 and @fro is not null
          set @rtn = @rtn + @fro + ' '
    
        set @rtn = @rtn + @to + ' '
    
        set @cnt = @cnt + 1
        fetch next from cr into @fro, @to
      end
    
      close cr
      deallocate cr
    
      set @rtn = left(@rtn, datalength(@rtn) - 1)
    
      return @rtn
    end
    go
    

    It is more efficient if you only call the function one time per unique EVENT_ID, so we select the distinct EVENT_ID in a sub-query:

    select x.EVENT_ID as [Event ID], dbo.fnConcat(x.EVENT_ID) as Movements
    from (
      select distinct EVENT_ID
      from tmp
    ) as x
    go
    

    And then cleanup:

    drop table tmp
    go
    drop function fnConcat
    go
    

    This is the result looks like this:

    Event ID    Movements
    ----------- ---------------------------
    55001       05 26 28 27 26 27 28 30 40
    56215       06 11 22
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database table called Participant that looks something like this: (idParticipant)
In my application I have a sqlite database that looks like this: CREATE TABLE
I have some code to update a database table that looks like try {
I have a self-referential table in my database that looks sort of like above.
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have data in an SQLite table that looks like this: user_id event_date ----------
I have a html table that looks something like this <table id=eventTable> <thead> <tr><th>#</th><th>Options</th></tr>
I have multiple tables in a MySQL database. Lets say they look like this:
that my problem: I have database table like that: id (AI) market_id 1 6
I have 2 related database tables which in simplified form look like this Product(

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.