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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:28:14+00:00 2026-06-07T00:28:14+00:00

I’m working with airline data, so I have a query like this SELECT Invoices.PNR,

  • 0

I’m working with airline data, so I have a query like this

SELECT Invoices.PNR, Segments.Depart, Segments.Arrival, Segments.DepartDateTime
FROM Invoices AS i INNER JOIN Segments AS s
ON i.Invoice_ID = s.Invoice_ID`
WHERE PNR = 'AAAAAA'

This returns

PNR    Depart Arrival DepartDateTime
AAAAAA DFW    MCI     7/2/2012 7:30 AM
AAAAAA MCI    LAX     7/2/2012 11:30 AM
AAAAAA LAX    DFW     7/4/2012 2:30 PM

I have a column in Invoices called routing that I want to show ‘DFW-MCI-LAX-DFW’ Is this possible using a SQL only method ? The segments are listed in order, so DFW-MCI is first then MCI-LAX then LAX-DFW.

EDIT: If I could update the database with DFW-MCI-MCI-LAX-LAX-DFW that is perfectly acceptable. I can strip out the duplicate entries on the view layer.

I can easily write this in ColdFusion, but the looping and thousands of database updates takes forever. I could also do a mass update for every 100 records, but I’d like to avoid using anything other than SQL altogether

  • 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-07T00:28:16+00:00Added an answer on June 7, 2026 at 12:28 am

    Here’s how you can update the values, assuming this is a one-time update (if it’s not, you’re going to have to run this every time Segments has any changes):

    ;WITH x AS 
    (
      SELECT Invoice_ID, Routing = STUFF
      (
        (SELECT '-' + Depart + '-' + Arrival
          FROM dbo.Segments
          ORDER BY DepartDateTime
          FOR XML PATH(''), 
          TYPE).value(N'./text()[1]', N'varchar(max)'), 1, 1, '')
      FROM dbo.Segments
      GROUP BY Invoice_ID
    )
    UPDATE i SET Routing = x.Routing
      FROM dbo.Invoices AS i
      INNER JOIN x ON i.Invoice_ID = x.Invoice_ID;
    

    Content of Invoices after the update:

    PNR Invoice_ID Routing
    AAAAAA 1 DFW-MCIMCI-LAXLAX-DFW
    • Example db<>fiddle

    If you are on SQL Server 2017 or greater, this is a lot easier with STRING_AGG():

    ;WITH cte(rt, id) AS
    (
      SELECT STRING_AGG(CONCAT_WS('-', Depart, Arrival), '')
             WITHIN GROUP (ORDER BY DepartDateTime), Invoice_ID
      FROM dbo.Segments GROUP BY Invoice_ID
    )
    UPDATE i SET i.Routing = cte.rt
    FROM dbo.Invoices AS i 
    INNER JOIN cte ON i.Invoice_ID = cte.id;
    
    • Example db<>fiddle
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.