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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:55:50+00:00 2026-06-17T17:55:50+00:00

I’ve got a SQL table similar to this: +———————————————–+ | ID | FirstName |

  • 0

I’ve got a SQL table similar to this:

+-----------------------------------------------+
|   ID   | FirstName | LastName  | SomeOtherData|
+-----------------------------------------------+
|  200   | Robert    | Barone    | Foo          |
|  228   | Doug      | Heffernan | Bar          |
|  2091  | Robert    | Barone    | Foo          |
|  3921  | Doug      | Heffernan | Bar          |
|  291   | Greg      | Warner    | Barfoo       |
+-----------------------------------------------+

Now what I’m having trouble producing is a table that’ll list both IDs for a given Person, assuming that FirstName and LastName are used to indicate duplicates. So, basically I’m trying to get:

+---------------------------------------------------------+
|   ID   | OtherID | FirstName | LastName  | SomeOtherData|
+---------------------------------------------------------+
|  200   | 2091    | Robert    | Barone    | Foo          |
|  228   | 3921    | Doug      | Heffernan | Bar          |
|  291   |         | Greg      | Warner    | Barfoo       |
+---------------------------------------------------------+

Would anyone be able to help me out with something like this? Thanks!

  • 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-17T17:55:51+00:00Added an answer on June 17, 2026 at 5:55 pm

    You can use a PIVOT which will transform the data from rows into columns:

    select [1] Id,
      [2] OtherId,
      firstname, 
      lastname
    from
    (
      select id, firstname, lastname,
        row_number() over(partition by firstname, lastname
                          order by id) rn
      from yourtable
    ) src
    pivot
    (
      max(id)
      for rn in ([1], [2])
    ) piv
    

    See SQL Fiddle with Demo

    Or you could use an aggregate function with a CASE expression:

    select 
      max(case when rn = 1 then id end) Id,
      max(case when rn = 2 then id end) OtherId,
      firstname,
      lastname
    from
    (
      select id, firstname, lastname,
        row_number() over(partition by firstname, lastname
                          order by id) rn
      from yourtable
    ) src
    group by firstname, lastname
    

    The above will work great if you have a known number of duplicate values (1, 2, etc). You could also implement dynamic SQL if you have more than 2 id’s. The dynamic SQL would look like:

    DECLARE @cols AS NVARCHAR(MAX),
        @colNames AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(cast(row_number() over(partition by firstname, lastname order by id) as varchar(50))) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    select @colNames = STUFF((SELECT distinct ', ' + QUOTENAME(cast(row_number() over(partition by firstname, lastname order by id) as varchar(50))) +' as Id_' + cast(row_number() over(partition by firstname, lastname order by id) as varchar(50)) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT ' + @colNames + ', firstname, lastname from 
                 (
                    select id, firstname, lastname,
                      row_number() over(partition by firstname, lastname
                                        order by id) rn
                    from yourtable
                ) x
                pivot 
                (
                    max(id)
                    for rn in (' + @cols + ')
                ) p 
                '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    The result of all 3 would be:

    |  ID | OTHERID | FIRSTNAME |  LASTNAME |
    -----------------------------------------
    | 200 |    2091 |    Robert |    Barone |
    | 228 |    3921 |      Doug | Heffernan |
    | 291 |  (null) |      Greg |    Warner |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
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 have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this

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.