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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:04:43+00:00 2026-05-30T22:04:43+00:00

Suppose there was a SQL table like this: Label Name | Last name |

  • 0

Suppose there was a SQL table like this:

Label Name | Last name | Address Line 1 | Address line 2
-----------+-----------+----------------+-------------------
John       | Smith     | 100 Any St     | Anytown, WI, 50000
Jane       | Smith     | 100 Any St     | Anytown, WI, 50000
Bill       | Doe       | 200 Any St     | Anytown, WI, 50000
Sam        | Rodgers   | 200 Any St     | Anytown, WI, 50000
Lisa       | Thompson  | 300 Any St     | Anytown, WI, 50000
Walter     | Thompson  | 300 Any St     | Anytown, WI, 50000
Chuck      | Thompson  | 300 Any St     | Anytown, WI, 50000

What SQL query would produce a table like this?:

Label Name              | Last name | Address Line 1 | Address line 2
------------------------+-----------+----------------+-------------------
John and Jane           | Smith     | 100 Any St     | Anytown, WI, 50000
Bill                    | Doe       | 200 Any St     | Anytown, WI, 50000
Sam                     | Rodgers   | 200 Any St     | Anytown, WI, 50000
Lisa, Walter, and Chuck | Thompson  | 300 Any St     | Anytown, WI, 50000
  • 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-30T22:04:44+00:00Added an answer on May 30, 2026 at 10:04 pm

    You should do these kinds of things in the presentation layer, rather than in the DBMS.

    But if you really want to do this at the DBMS level, and your DBMS is MS SQL Server (as your comment seems to indicate), you can do the following:

    First, create a function that concatenates related [Label Name] values (using ‘,’ and ‘and’ as separators):

    CREATE FUNCTION CombineNames (
        @LastName nvarchar(max),
        @AddressLine1 nvarchar(max),
        @AddressLine2 nvarchar(max)
    )
    RETURNS nvarchar(max) AS
    BEGIN
    
        DECLARE @NameCount INT;
    
        SELECT @NameCount = COUNT(*)
        FROM YOUR_TABLE
        WHERE
            [Last name] = @LastName
            AND [Address Line 1] = @AddressLine1
            AND [Address line 2] = @AddressLine2;
    
        DECLARE @List nvarchar(max);
    
        SELECT
            @List
                = COALESCE(@List, '')
                + (
                    CASE ROW_NUMBER() OVER(ORDER BY [Label Name])
                    WHEN 1 THEN ''
                    WHEN @NameCount THEN ' and '
                    ELSE ', '
                    END
                )
                + CAST([Label Name] as nvarchar(max))
        FROM YOUR_TABLE
        WHERE
            [Last name] = @LastName
            AND [Address Line 1] = @AddressLine1
            AND [Address line 2] = @AddressLine2
        ORDER BY
            [Label Name];
    
        RETURN (SELECT @List)
    
    END
    

    Then use the function like this:

    SELECT
        dbo.CombineNames([Last name], [Address Line 1], [Address Line 2]),
        T.*
    FROM
        (
            SELECT DISTINCT [Last name], [Address Line 1], [Address Line 2]
            FROM YOUR_TABLE
        ) T
    

    Result:

    (No column name)        Last name       Address Line 1      Address Line 2
    ----------------        ---------       --------------      --------------
    Bill                    Doe             200 Any St          Anytown, WI, 50000
    Sam                     Rodgers         200 Any St          Anytown, WI, 50000
    Jane and John           Smith           100 Any St          Anytown, WI, 50000
    Chuck, Lisa and Walter  Thompson        300 Any St          Anytown, WI, 50000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an SQL table entitled Facility which looks like this: || FacilityID
Suppose I have a table like this ID Name 1 RJ 2 Hello 3
Suppose we have this person table id name gender 1 John male 2 Kurt
Suppose I have simple table like this: ID (PRIMARY) time (INT) stage (TINYINT) other
Suppose there're 2 tables with data like below: -- Table #Detail TelNO ---------- 001xxxxx
Suppose there is a table A which has a column AccessRights which is multivalued(
Suppose there is a System A which gives some output. This output is used
I suppose there could be historical reasons for this naming and that other languages
Suppose I have a table with full-text index on column Firstname, lastname, email. There
Suppose I have a table that contains valid data. I would like to modify

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.