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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:17:05+00:00 2026-05-14T14:17:05+00:00

I got child / parent tables as below. MasterTable: MasterID, Description ChildTable ChildID, MasterID,

  • 0

I got child / parent tables as below.

MasterTable:

MasterID, Description

ChildTable

ChildID, MasterID, Description.

Using PIVOT / UNPIVOT how can i get result as below in single row.

if (MasterID : 1 got x child records)

MasterID, ChildID1, Description1, ChildID2, Description2....... ChildIDx, Descriptionx

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-05-14T14:17:05+00:00Added an answer on May 14, 2026 at 2:17 pm

    here is a T_SQL, assuming this:

    • You do not know how many columns may appear in the results.
    • The Pivot elements can vary (thats why the first assumption).
    • You need the specific order ‘ChildId1, ChilDesc1, ChildId2, ChildDesc2… asd so ever’

    DECLARE
    @MaxCountOfChild int

    -- Obtaining Maximum times a Master is used by its children
    SELECT TOP 1 @MaxCountOfChild= count(*)
    FROM ChildTable
    GROUP BY MasterID
    order by count(*) DESC
    
    
    --With that number, create a string for the Pivot elements
    --if you want them in the order Id1-Desc1-Id2-Desc2
    DECLARE 
        @AuxforReplacing nvarchar(MAX),
        @ChildIdsandDescs nvarchar(MAX),
        @PivotElements nvarchar(MAX),
        @Counter int,
        @sql nvarchar(MAX)
    
    SET @Counter=0
    SET @AuxforReplacing=''
    SET @ChildIdsandDescs=''
    SET @PivotElements=''
    
    WHILE (@Counter < @MaxCountOfChild)
    begin
        SET @Counter=@Counter +1
        SET @PivotElements=@PivotElements + '[' +convert(varchar, @Counter)+ '],' 
        SET @AuxforReplacing=@AuxforReplacing +  '[' +convert(varchar, @Counter)+ '] as ' + convert(varchar, @Counter) + ','
        SET @ChildIdsandDescs=@ChildIdsandDescs + '[ChildID' + convert(varchar, @Counter)+ '],[ChildDesc' + convert(varchar, @Counter) +'],'
    
    end
    SET @PivotElements=LEFT(@PivotElements, len(@PivotElements)-1)
    SET @ChildIdsandDescs=LEFT(@ChildIdsandDescs, len(@ChildIdsandDescs)-1)
    SET @AuxforReplacing=LEFT(@AuxforReplacing, len(@AuxforReplacing)-1)
    
    
    --print REPLACE(@AuxforReplacing, 'as ', 'as ChildId')
    
    --print @ChildIds
    --print @PivotElements
    
    
    SET @sql = N'
    WITH AuxTable (Masterdesc,ChildId, MasterId,ChildDesc,  NumeroenMaster) 
    AS
    (
    SELECT M.Description as MasterDesc, C.*, RANK() OVER (PARTITION BY M.MasterId ORDER BY M.MasterId, ChildId)
    FROM  MasterTable M
        INNER JOIN ChildTable C
            ON M.MasterId=C.MasterId
    )
    
    SELECT TablaMaster.MasterId,' + @ChildIdsandDescs + '
    FROM 
    (
        SELECT MasterId, ' + REPLACE(@AuxforReplacing, 'as ', 'as ChildId') + '
        FROM (
        SELECT MasterId, NumeroenMaster, ChildId
        FROM AuxTable) P
        PIVOT
        (
        MAX (ChildId)
        FOR NumeroenMaster IN (' + @PivotElements +')
        ) AS pvt) As TablaMaster
    INNER JOIN 
    (
        SELECT MasterId, ' + REPLACE(@AuxforReplacing, 'as ', 'as ChildDesc') + '
        FROM (
        SELECT MasterId, NumeroenMaster, ChildDesc
        FROM AuxTable) P
        PIVOT
        (
        MAX (ChildDesc)
        FOR NumeroenMaster IN (' + @PivotElements +')
        ) AS pvt) As TablaChild
    ON TablaMaster.MasterId= TablaChild.MasterId'
    
    EXEC sp_executesql @sql
    

    EDIT: The result is this:

    MasterId ChildID1 ChildDesc1 ChildID2 ChildDesc2  ChildID3 ChildDesc3 ChildID4 ChildDesc4
    -------- -------- ---------- -------- ----------- -------- ---------- -------- ---------
    1           1      Child1       2      Child2     NULL        NULL       NULL      NULL
    2           3      Child3       4      Child4      7          Child7      8      Child8
    3           5      Child5       6      Child5     NULL        NULL       NULL      NULL
    
    Asumming this in the table ChildTable:
    ChildId  MasterId  ChildDesc
    -------  --------  ---------
    1      1       Child1
    2      1       Child2
    3      2       Child3
    4      2       Child4
    5      3       Child5
    6      3       Child5
    7      2       Child7
    8      2       Child8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.