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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:18:38+00:00 2026-06-17T14:18:38+00:00

I have a table that has rows with unique values in one column. How

  • 0

I have a table that has rows with unique values in one column. How can I place these in a row so that one entry has multiple entries?

For example here is a table…

create table visit_view
(
  last_name varchar(25),
  first_name varchar(25),
  middle_name varchar(25),
  dob datetime,
  di datetime,
  m varchar(20),
  d varchar(12),
  d_sequence_num smallint
)

insert into visit_view values ('LEE','BUDDY','','05/20/2010','10/01/2012','123456','786.2','2') 
insert into visit_view values   ('LEE','BUDDY','','05/20/2010','10/01/2012','123456','784.99','3') 
insert into visit_view values ('TU','BIBO','LU','09/29/2012','10/01/2012','321456','774.6','1') 
insert into visit_view values ('SMITH','BOBBIE','JOE','09/29/2012','10/01/2012','321654','V50.2','1')
insert into visit_view values ('LEWIS','CAREY','','11/11/2011','10/01/2012','654123','057.9','2')
insert into visit_view values ('LEWIS','CAREY','','11/11/2011','10/01/2012','654123','074.3','3')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','381.81','2')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','786.09','3')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','380.4','4')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','478.19','5')

And my query looks like this…

SELECT  
    first_name AS FirstName, 
    ISNULL(middle_name, '') AS MI, 
    last_name AS LastName, 
    CONVERT(varchar,dob, 101) DOB,
    CONVERT(varchar,di,101) DCdate,
    CAST(m AS INT) AS MR,
    d AS Diag
FROM 
    visit_view 
WHERE 
    d_sequence_num>1
    AND DATEDIFF(year,dob,GETDATE()) <= 3
ORDER BY di,d_sequence_num ASC

I my output is

FIRSTNAME MI LASTNAME DOB DCDATE MR DIAG 
CAREY  LEWIS 11/11/2011 10/01/2012 654123 057.9 
BUDDY  LEE 05/20/2010 10/01/2012 123456 786.2 
BUDDY  LEE 05/20/2010 10/01/2012 123456 784.99 
CAREY  LEWIS 11/11/2011 10/01/2012 654123 074.3 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 381.81 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 786.09 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 380.4 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 478.19 

But I want it like this…

FIRSTNAME MI LASTNAME DOB DCDATE MR DIAG 
CAREY  LEWIS 11/11/2011 10/01/2012 654123 057.9 
BUDDY  LEE 05/20/2010 10/01/2012 123456 786.2 784.99
CAREY  LEWIS 11/11/2011 10/01/2012 654123 074.3 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 381.81 786.09 380.4 478.19 
  • 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-17T14:18:39+00:00Added an answer on June 17, 2026 at 2:18 pm

    I am not sure why you have CAREY LEWIS showing twice in your result and HECTOR RAMIREZ once, but you can consolidate the rows using something like this:

    SELECT  
        first_name AS FirstName, 
        ISNULL(middle_name, '') AS MI, 
        last_name AS LastName, 
        CONVERT(varchar,dob, 101) DOB,
        CONVERT(varchar,di,101) DCdate,
        CAST(m AS INT) AS MR,
        stuff((select distinct ', '+ d 
               from visit_view v1
               where v.first_name = v1.first_name
               FOR XML PATH('')),1,1,'') Diag   
    FROM visit_view v
    WHERE d_sequence_num>1
        AND DATEDIFF(year,dob,GETDATE()) <= 3
    GROUP BY first_name, middle_name, last_name, dob, di, m
    ORDER BY di ASC
    

    See SQL Fiddle with Demo

    Gives the result:

    | FIRSTNAME | MI | LASTNAME |        DOB |     DCDATE |     MR |                           DIAG |
    -------------------------------------------------------------------------------------------------
    |     BUDDY |    |      LEE | 05/20/2010 | 10/01/2012 | 123456 |                  784.99, 786.2 |
    |     CAREY |    |    LEWIS | 11/11/2011 | 10/01/2012 | 654123 |                   057.9, 074.3 |
    |    HECTOR |    |  RAMIREZ | 04/21/2011 | 10/02/2012 | 654321 |  380.4, 381.81, 478.19, 786.09 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that has multiple rows with the following fields: PersonName SongName
I have one table that has two fields - ID1 and ID2 ID1 can
I have a table which has two fields. There can be multiple rows with
I have a logging table which has three columns. One column is a unique
I have a SQL Server table in production that has millions of rows, and
I have a large table (~1M rows now, soon ~10M) that has two ranked
I have two tables, one that has 450 rows, the other is a Sort
I have a table that has two datetime columns (one for start time and
I have a table that has no primary key, and one cannot be created
I have a larger dataset (data.table with approx 9m rows) with a column that

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.