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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:22:00+00:00 2026-06-15T02:22:00+00:00

The problem I’m having is that there are more than one result in a

  • 0

The problem I’m having is that there are more than one result in a table for what I’m trying to find. So if I have this:

SELECT DISTINCT student.ident AS [Ident],
     proghist.prgc AS [Test Code]
FROM student 
LEFT OUTER JOIN proghist

For some students I get multiple “Test Codes” so it’ll look like this:

Ident   Test Code
123456   1
123456   4
654321   2
654321   6
122222   1

Is there a way to combine them to one row and separate columns?

Edit: I would like the data to be in the final result:

123456 1 4 
654321 2 6 
122222 1
  • 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-15T02:22:01+00:00Added an answer on June 15, 2026 at 2:22 am

    Since you are using SQL Server, if you are using SQL Server 2005+, then you can use the PIVOT function. If you know you will only have up to two TestCodes per ident then you can hard-code the values:

    select ident,
      [1] TestCode1,
      [2] TestCode2
    from
    (
      SELECT  s.ident AS Ident,
        p.prgc AS TestCode,
        row_number() over(partition by s.ident order by p.prgc) rn
      FROM student s
      LEFT OUTER JOIN proghist p
        on s.ident = p.ident
    ) src
    pivot
    (
      max(TestCode)
      for rn in ([1], [2])
    ) piv
    

    See SQL Fiddle with Demo

    If you have an unknown number of values of TestCodes, then you can use dynamic SQL to PIVOT the data:

    DECLARE @cols AS NVARCHAR(MAX),
        @colsPivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(rn)
                        FROM
                        (
                          select cast(row_number() over(partition by s.ident order by p.prgc) as varchar(50)) rn
                          FROM student s
                          LEFT OUTER JOIN proghist p
                            on s.ident = p.ident
                        ) src
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @colsPivot = STUFF((SELECT distinct ',' + QUOTENAME(rn) + ' as TestCode'+rn
                        FROM
                        (
                          select cast(row_number() over(partition by s.ident order by p.prgc) as varchar(50)) rn
                          FROM student s
                          LEFT OUTER JOIN proghist p
                            on s.ident = p.ident
                        ) src
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT Ident, ' + @colsPivot + ' from 
                 (
                   SELECT  s.ident AS Ident,
                    p.prgc AS TestCode,
                    row_number() over(partition by s.ident order by p.prgc) rn
                  FROM student s
                  LEFT OUTER JOIN proghist p
                    on s.ident = p.ident
                ) src
                pivot 
                (
                    max(TestCode)
                    for rn in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    If you do not have access to the PIVOT function, then you can use an aggregate function with a CASE statement:

    select ident,
      max(case when rn = 1 then testcode else '' end) TestCode1,
      max(case when rn = 2 then testcode else '' end) TestCode2
    from
    (
      SELECT  s.ident AS Ident,
        p.prgc AS TestCode,
        row_number() over(partition by s.ident order by p.prgc) rn
      FROM student s
      LEFT OUTER JOIN proghist p
        on s.ident = p.ident
    ) src
    group by ident
    

    See SQL Fiddle with Demo

    All three will produce the same result:

    |  IDENT | TESTCODE1 | TESTCODE2 |
    ----------------------------------
    | 122222 |         1 |         0 |
    | 123456 |         1 |         4 |
    | 654321 |         2 |         6 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem is like this: I have two winapi application's. There is only one way
Problem: I have a table that prints out vertical but I would like it
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem description I have 6 databases from 6 different machines, and having one cloud
Problem: I have two array where one produce a category and the second produce
Problem is like this: Suppose there are records in dataTable with FruitType as A
Problem: Find people whose birthdays are tomorrow (table a), who havent got a record
Problem background: I have a Qt/QML Symbian application targeting Qt 4.7.4, that requires a
Problem: I have a WCF service that invokes methods via reflection and sends the

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.