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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:10:52+00:00 2026-05-23T06:10:52+00:00

Sorry for the title, not easy to phrase it correctly. I have a table

  • 0

Sorry for the title, not easy to phrase it correctly.

I have a table A which has a business key (Akey) with possible value range : k1, k2 and k3

AKey ACol2  ACol3
k1   val12  val13
k2   val22  val23
k3   val32  val33

I have a table B which has entries like

BKey1 BKey2 BKey3  AKey Col5  Col6
kk11  kk12  kk13   k1   val15 val16
kk21  kk22  kk23   k3   val25 val26

BKey1, BKey2 and Bkey3 is the business key of table B
Akey is a foreign key to table A

I would like to generate a result set with table B joined with table A (on AKey) but include “empty” lines for non referenced key from table A (and this for each key of table B). In the example I would like to have :

BKey1 BKey2 BKey3  AKey Col5  Col6   ACol2 ACol3
kk11  kk12  kk13   k1   val15 val16  val12 val13
kk21  kk22  kk23   k3   val25 val26  val32 val33
kk11  kk12  kk13   k2   def   def    val22 val23
kk11  kk12  kk13   k3   def   def    val32 val33
kk21  kk22  kk23   k1   def   def    val12 val13
kk21  kk22  kk23   k2   def   def    val22 val23

I have two entries just like for the inner join + entries for all other key range values of A (k1, k2 and k3) with default values (that I need to set) for columns of B. I tried a right join but then I only have default entries once in the result and not for each key of B. Can you help me in the right direction to achieve this ?

Christian

  • 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-23T06:10:53+00:00Added an answer on May 23, 2026 at 6:10 am
    select D.BKey1,
           D.BKey2,
           D.BKey3,
           D.AKey,
           coalesce(B.Col5, 'def') as Col5,
           coalesce(B.Col6, 'def') as Col6,
           A.ACol2,
           A.ACol3
    from (select B.BKey1, B.BKey2, B.BKey3, A.AKey
          from TableA as A
            cross join (select distinct BKey1, BKey2, BKey3
                        from TableB
                       ) as B
         ) as D
      inner join TableA as A
        on A.AKey = D.AKey
      left outer join TableB as B
        on B.BKey1 = D.BKey1 and 
           B.BKey2 = D.BKey2 and 
           B.BKey3 = D.BKey3 and 
           B.AKey = D.AKey
    

    For test:

    with TableA(AKey, ACol2,  ACol3) as
    (
    select 'k1',   'val12',  'val13' union all
    select 'k2',   'val22',  'val23' union all
    select 'k3',   'val32',  'val33'
    ),
    TableB(BKey1, BKey2, BKey3,  AKey, Col5,  Col6) as
    (
    select 'kk11',  'kk12',  'kk13',   'k1',   'val15', 'val16' union all
    select 'kk11',  'kk12',  'kk13',   'k2',   'valx',  'valy'  union all
    select 'kk21',  'kk22',  'kk23',   'k3',   'val25', 'val26'
    )
    
    select D.BKey1,
           D.BKey2,
           D.BKey3,
           D.AKey,
           coalesce(B.Col5, 'def') as Col5,
           coalesce(B.Col6, 'def') as Col6,
           A.ACol2,
           A.ACol3
    from (select B.BKey1, B.BKey2, B.BKey3, A.AKey
          from TableA as A
            cross join (select distinct BKey1, BKey2, BKey3
                        from TableB
                       ) as B
         ) as D
      inner join TableA as A
        on A.AKey = D.AKey
      left outer join TableB as B
        on B.BKey1 = D.BKey1 and 
           B.BKey2 = D.BKey2 and 
           B.BKey3 = D.BKey3 and 
           B.AKey = D.AKey
    

    Result:

    BKey1 BKey2 BKey3 AKey Col5  Col6  ACol2 ACol3
    ----- ----- ----- ---- ----- ----- ----- -----
    kk11  kk12  kk13  k1   val15 val16 val12 val13
    kk11  kk12  kk13  k2   valx  valy  val22 val23
    kk11  kk12  kk13  k3   def   def   val32 val33
    kk21  kk22  kk23  k1   def   def   val12 val13
    kk21  kk22  kk23  k2   def   def   val22 val23
    kk21  kk22  kk23  k3   val25 val26 val32 val33
    

    Test it here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the slightly rubbish title. I could not think how to describe this
I'm sorry I could not think of a better title. The problem is the
Sorry the title isn't more help. I have a database of media-file URLs that
First of all, sorry about that title. I'm not the best at writing those
Sorry for what is a generic title. I'm not the best at titles. Anyway
Okay the question title may not have made sense... mostly because I don't know
Sorry if the title is not very clear. I was not sure about the
Sorry about the title. I do realize it's not very descriptive. :| Here is
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry for the long question title. I guess I'm on to a loser on

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.