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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:51:09+00:00 2026-05-11T19:51:09+00:00

I have some data id ref == ========== 1 3536757616 1 3536757617 1 3536757618

  • 0

I have some data

id    ref
==   ==========
1    3536757616
1    3536757617
1    3536757618

and want to get the result

1    3536757616/7/8

so essentially the data is aggregated on id, with the refs concatenated together, separated by a slash ‘/’, but with any common prefix removed so if the data was like

id    ref
==   ==========
2    3536757628
2    3536757629
2    3536757630

I would want to get the result

2    3536757629/28/30

I know I can simply concatenate the refs by using

SELECT distinct
    id,
    stuff ( ( SELECT
                  '/ ' + ref 
              FROM
                  tableA tableA_1
              where tableA_1.id = tableA_2.id
    FOR XML PATH ( '' ) ) , 1 , 2 , '' )
from TableA tableA_2

to give

1   3536757616/ 3536757617/ 3536757618
2   3536757628/ 3536757629/ 3536757630

but it’s the bit that removes the common element that I’m after…..


Code for test data :

create table tableA (id int, ref varchar(50))

insert into tableA
select 1, 3536757616
union select 1, 3536757617
union select 1, 3536757618
union select 2, 3536757628
union select 2, 3536757629
union select 2, 3536757630
  • 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-11T19:51:10+00:00Added an answer on May 11, 2026 at 7:51 pm
    WITH hier(cnt) AS
            (
            SELECT  1
            UNION ALL
            SELECT  cnt + 1
            FROM    hier
            WHERE   cnt <= 100
            )
    SELECT  CASE WHEN ROW_NUMBER() OVER (ORDER BY id) = 1 THEN ref ELSE ' / ' + SUBSTRING(ref, mc + 1, LEN(ref)) END 
    FROM    (
            SELECT  MIN(common) AS mc
            FROM    (
                    SELECT  (
                            SELECT  MAX(cnt)
                            FROM    hier
                            WHERE   SUBSTRING(initref, 1, cnt) = SUBSTRING(ref, 1, cnt)
                                    AND cnt <= LEN(ref)
                            ) AS common
                    FROM    (
                            SELECT  TOP 1 ref AS initref
                            FROM    tableA
                            ) i,
                            tableA
                    ) q
            ) q2, tableA
    FOR XML PATH('')
    
    ---
    
    3536757616 / 17 / 18 / 28 / 29 / 30
    

    Same thing with groups:

    WITH hier(cnt) AS
            (
            SELECT  1
            UNION ALL
            SELECT  cnt + 1
            FROM    hier
            WHERE   cnt <= 100
            )
    SELECT  (
            SELECT  CASE WHEN ROW_NUMBER() OVER (ORDER BY a2.ref) = 1 THEN ref ELSE ' / ' + SUBSTRING(ref, mc + 1, LEN(ref)) END 
            FROM    tableA a2
            WHERE   a2.id = q2.id
            FOR XML PATH('')
            )
    FROM    (
            SELECT  id, MIN(common) AS mc
            FROM    (
                    SELECT  a.id,
                            (
                            SELECT  MAX(cnt)
                            FROM    hier
                            WHERE   SUBSTRING(i.initref, 1, cnt) = SUBSTRING(a.ref, 1, cnt)
                                    AND cnt <= LEN(ref)
                            ) AS common
                    FROM    (
                            SELECT  id, MIN(ref) AS initref
                            FROM    tableA
                            GROUP BY
                                    id
                            ) i
                    JOIN    tableA a
                    ON      i.id = a.id
                    ) q
            GROUP BY
                    id
            ) q2
    ---
    3536757616 / 7 / 8
    3536757628 / 29 / 30
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data. I want to go through that data and change cells
I have some data that I want to store somewhere in my Rails app
I have some data stored as ArrayList . And when I want to backup
So we have unique 'codes' for some of our grails objects (ref data), and
i have some data to write in data base (three strings); i have a
I have some data with messed-up accented characters. For example in the data we
I have some data grouped in a table by a certain criteria, and for
I have some data of the form Key ID Link 1 MASTER 123 2
I have some data files to import into a database with some unique delimiters:
I have some data with one row of headers and one or more rows

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.