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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:59:56+00:00 2026-05-20T09:59:56+00:00

I need to combine one Header Table and six Detail Tables into a single

  • 0

I need to combine one Header Table and six Detail Tables into a single result. To demonstrate I have created a very simple example as follows:

DECLARE @MyHeader TABLE (HeaderPK int, Name varchar(100), Total smallmoney)
INSERT INTO @MyHeader (HeaderPK, Name, Total) 
    SELECT 1, 'ABC Company', 1600

DECLARE @MyDetail1 TABLE (Detail1PK int, HeaderFK int, Detail1Description varchar(100), Detail1Amount smallmoney)
INSERT INTO @MyDetail1 (Detail1PK, HeaderFK, Detail1Description, Detail1Amount) 
    SELECT 1, 1, 'Detail 1A', 100
    UNION SELECT 2, 1, 'Detail 1B', 300

DECLARE @MyDetail2 TABLE (Detail2PK int, HeaderFK int, Detail2Description varchar(100), Detail2AmountA smallmoney, Detail2AmountB smallmoney)
INSERT INTO @MyDetail2 (Detail2PK, HeaderFK, Detail2Description, Detail2AmountA, Detail2AmountB) 
    SELECT 1, 1, 'Detail 2A', 100, 100
    UNION SELECT 2, 1, 'Detail 2B', 200, 200
    UNION SELECT 3, 1, 'Detail 3C', 300, 300

-- Returns 2 Rows, Expected 2
SELECT 
    MyHeader.*
    ,MyDetail1.*
FROM
    @MyHeader MyHeader
    FULL JOIN @MyDetail1 MyDetail1 ON MyHeader.HeaderPK = MyDetail1.HeaderFK
ORDER BY
    MyDetail1.Detail1PK

-- Returns 6 Rows, Expected 3
SELECT 
    MyHeader.*
    ,MyDetail1.*
    ,MyDetail2.*
FROM
    @MyHeader MyHeader
    FULL JOIN @MyDetail1 MyDetail1 ON MyHeader.HeaderPK = MyDetail1.HeaderFK
    FULL JOIN @MyDetail2 MyDetail2 ON MyHeader.HeaderPK = MyDetail2.HeaderFK

Notes:

  • MSSQL 2008R2
  • Each Detail Table will have approximately 0 to 15 records.

In the sample, the Detail Tables have similar structures. In the production system, they are very different.

  • 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-20T09:59:57+00:00Added an answer on May 20, 2026 at 9:59 am

    This will give you what you want, based on a slot upwards retrieval of the detail tables. Frankly I am showing this code only because it is mildly interesting. There is normally no good reason to do this type of query.

    For transfer efficiency, this would probably be better not to repeat the data in the header columns:

    Header
    Detail1
    Detail2
    Detail3
    etc
    

    SQL Query

    DECLARE @MyHeader TABLE (HeaderPK int, Name varchar(100), Total smallmoney)
    INSERT INTO @MyHeader (HeaderPK, Name, Total) 
        SELECT 1, 'ABC Company', 1600
    
    DECLARE @MyDetail1 TABLE (Detail1PK int, HeaderFK int, Detail1Description varchar(100), Detail1Amount smallmoney)
    INSERT INTO @MyDetail1 (Detail1PK, HeaderFK, Detail1Description, Detail1Amount) 
        SELECT 1, 1, 'Detail 1A', 100
        UNION SELECT 2, 1, 'Detail 1B', 300
    
    DECLARE @MyDetail2 TABLE (Detail2PK int, HeaderFK int, Detail2Description varchar(100), Detail2AmountA smallmoney, Detail2AmountB smallmoney)
    INSERT INTO @MyDetail2 (Detail2PK, HeaderFK, Detail2Description, Detail2AmountA, Detail2AmountB) 
        SELECT 1, 1, 'Detail 2A', 100, 100
        UNION SELECT 2, 1, 'Detail 2B', 200, 200
        UNION SELECT 3, 1, 'Detail 3C', 300, 300
    
    DECLARE @MyDetail3 TABLE (Detail3PK int, HeaderFK int, Detail3Description sysname)
    INSERT INTO @MyDetail3 (Detail3PK, HeaderFK, Detail3Description) 
        SELECT 1, 1, 'Detail 3A'
        UNION SELECT 2, 1, 'Detail 3B'
    
    -- Returns 6 Rows, Expected 3
    SELECT 
        MyHeader.*
        ,MyDetail1.*
        ,MyDetail2.*
        ,MyDetail3.*
    FROM
        @MyHeader MyHeader
        LEFT JOIN
        (SELECT *, RN=ROW_NUMBER() over (order by Detail1PK) FROM @MyDetail1) MyDetail1
        FULL JOIN
        (SELECT *, RN=ROW_NUMBER() over (order by Detail2PK) FROM @MyDetail2) MyDetail2
        ON MyDetail1.HeaderFK = MyDetail2.HeaderFK AND MyDetail1.RN = MyDetail2.RN
        FULL JOIN
        (SELECT *, RN=ROW_NUMBER() over (order by Detail3PK) FROM @MyDetail3) MyDetail3
        ON COALESCE(MyDetail1.HeaderFK, MyDetail2.HeaderFK) = MyDetail3.HeaderFK
           AND COALESCE(MyDetail1.RN, MyDetail2.RN) = MyDetail3.RN
        ON MyHeader.HeaderPK = COALESCE(MyDetail1.HeaderFK, MyDetail2.HeaderFK)
    

    You expand into more detail tables by adding more to the COALSECE, so the 6th detail table would be

        FULL JOIN
        (SELECT *, RN=ROW_NUMBER() over (order by Detail6PK) FROM @MyDetail6) MyDetail6
        ON COALESCE(MyDetail1.HeaderFK, MyDetail2.HeaderFK, MyDetail3.HeaderFK,
            MyDetail4.HeaderFK, MyDetail5.HeaderFK) = MyDetail6.HeaderFK
           AND COALESCE(MyDetail1.RN, MyDetail2.RN, MyDetail3.RN, MyDetail4.RN, MyDetail5.RN) = MyDetail6.RN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three single-dimensional arrays and I need to combine them into one 3-dimensional
I have 3 byte arrays in C# that I need to combine into one.
I have 3 tables that I am trying to combine into one with a
I have two .htaccess files that I need to combine into one. The first
I need to create and combine several expressions for child entity into one to
I have a MySql database with three tables I need to combine in a
I've got a grip of PDFs that I need to combine into one using
I have multiple files of text that I need to read and combine into
I have to combine these two mySQL queries into one. I duplicated a solution
Basically, I will need to combine product data from multiple vendors into a single

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.