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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:28:55+00:00 2026-05-29T04:28:55+00:00

I am working on modifying a view and I’m stuck. My user has an

  • 0

I am working on modifying a view and I’m stuck.

My user has an old program that queries a database view that has parent (mother and father) data. The mother’s and father’s name shared a single record (id, fname, lname, fname2, lname2, etc…).

I built a new database and the table and corresponding view was changed to have user_id, parent_id, fname, and lname.

Parents share a user_id but have individual parent_id.

Anyway, I am trying to get results from (user_id, fname, lname) back to looking like (id, fname, lname, fname2, lname2) and got stuck. I wasn’t sure where to start so I got to the following code:

This is what I start with (excluding the parent_id):

user_id     fname          lname         expiration

4           Jane           Doe           2015-01-01 00:00:00.000
4           John           Doe           2015-01-01 00:00:00.000
5           Bill           Smith         2015-01-01 00:00:00.000
5           Mary           Smith         2015-01-01 00:00:00.000

This is the sql I am trying and got stuck with:

SELECT    ROW_NUMBER() OVER ( PARTITION BY parents.user_id ORDER BY parents.user_id ASC ) RowVersion ,
        dbo.parents.user_id ,
        fname ,
        lname ,
        '' AS fname2 ,
        '' AS lname2 ,
        ExpirationDate AS 'expiration'
FROM      dbo.parents
        INNER JOIN dbo.payment ON dbo.parents.user_id = dbo.payment.User_Id
UNION
SELECT    ROW_NUMBER() OVER ( PARTITION BY parents.user_id ORDER BY parents.user_id ASC ) RowVersion ,
        dbo.parents.user_id ,
        '' AS fname ,
        '' AS lname ,
        fname AS fname2 ,
        lname AS lname2 ,
        ExpirationDate AS 'expiration'
FROM      dbo.parents
        INNER JOIN dbo.payment ON dbo.parents.user_id = dbo.payment.User_Id
GROUP BY  parents.user_id ,
        fname ,
        lname ,
        ExpirationDate

This is the result of above sql;

RowVersion  user_id     fname          lname         fname2             lname2          expiration
1           4                                        John               Doe             2015-01-01 00:00:00.000
1           4           Jane           Doe                                              2015-01-01 00:00:00.000
2           4                                        Jane               Doe             2015-01-01 00:00:00.000
2           4           John           Doe                                              2015-01-01 00:00:00.000
1           5                                        Bill               Smith           2015-01-01 00:00:00.000
1           5           Mary           Smith                                            2015-01-01 00:00:00.000
2           5                                        Mary               Smith           2015-01-01 00:00:00.000
2           5           Bill           Smith                                            2015-01-01 00:00:00.000

This is what I need:

user_id     fname          lname         fname2             lname2          expiration
4           Jane           Doe           John               Doe             2015-01-01 00:00:00.000
5           Bill           Smith         Mary               Smith           2015-01-01 00:00:00.000

I feel like I’m close but I also feel like I went down the wrong road. Any suggestions?

  • 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-29T04:28:56+00:00Added an answer on May 29, 2026 at 4:28 am

    This should do:

    SELECT    
        ROW_NUMBER() OVER (PARTITION BY A.user_id ORDER BY A.user_id ASC ) RowVersion ,
        A.user_id ,
        A.fname AS fname,
        A.lname AS lname,
        B.fname AS fname2 ,
        B.lname AS lname2 ,
        C.ExpirationDate AS 'expiration'
    FROM    
        dbo.parents A
        INNER JOIN dbo.parents B ON A.user_id = B.user_id AND
               A.ParentId > B.ParentId -- here you have to be careful fname and lname will be randomly male or female, if you provided other fields in parents table possibly there is a way to make it deterministic
        INNER JOIN dbo.payment C ON A.user_id = C.user_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a web interface for modifying Oracle database backup settings. One of
I'm working on modifying a website which has a chart of FAQs which have
I'm currently working on modifying a Firebird v. 1.5 database. The database structure will
I'm modifying a standard webcontrol in a system i'm working on which has some
I'm working on a database driven website (I didn't code it, just modifying few
I am working on modifying a relatively large C++ program, where unfortunately it is
I'm working on modifying some existing code for a payment gateway and I'm not
A common task in programs I've been working on lately is modifying a text
In the project my team is currently working on, we're modifying a commercial PHP
Working with a SqlCommand in C# I've created a query that contains a IN

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.