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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:40:31+00:00 2026-06-16T06:40:31+00:00

I have the following two tables. I am using SQL Server 2008 R2 Create

  • 0

I have the following two tables. I am using SQL Server 2008 R2

Create table #tmp1 (
a char(1)
)

Create table #tmp2 (
id int,
a char(1),
val int
)

insert #tmp1 values ('A')
insert #tmp1 values ('B')
insert #tmp1 values ('C')

insert #tmp2 values (1,  'A', 10)
insert #tmp2 values (1,  'B', 20)
insert #tmp2 values (2,  'A', 30)
insert #tmp2 values (2,  'C', 40)

select * from #tmp1 t1 left outer join #tmp2 t2 on t1.a = t2.a
order by t2.id

This returns the result set

A   1   A   10
B   1   B   20
C   2   C   40
A   2   A   30

I would like to have the following result set

 A     1    A       10
 B     1    B       20
 C     1    null    null
 A     2    A       30
 B     2    null    null
 C     2    C       40

Right now i am acheiving this by creating a new table with a cross join like this and then doing a outer join

select * into #tmp3 from #tmp1 cross join (select distinct ID from #tmp2) t
select * from #tmp3 t1 left outer join #tmp2 t2 on t1.a = t2.a and t1.id = t2.id

Is there a better way to do this ?

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-06-16T06:40:32+00:00Added an answer on June 16, 2026 at 6:40 am

    To get what you want, you need a “driving” table. That is, you want a complete list of all combinations, and then to join to the other tables to get the matches. Here is one way:

    select t1.a, t2.*
    from (select t1.a as a, t2.id as id
          from (select distinct a from #tmp1 t1) t1
               cross join
               (select distinct id from #tmp2 t2) t2
         ) driving left outer join
         #tmp1 t1
         on t1.a = driving.a left outer join
         #tmp2 t2
         on t2.id = driving.id and
            t2.a = driving.a
    order by t2.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: SQL Server 2008 R2 SSRS 2008 Humble Programmer Consider two tables: CREATE TABLE
Using SQL Server 2008 (R2). I have two tables in different databases (with same
I have two tables in SQL server for customers and their addresses. CREATE TABLE
I am using VB.NET with LINQ to MS SQL. I have two following tables.
Using SQL Server 2K8 R2, I have two related tables - Member and Questionnaire.
I want to merge two tables using SQL Server on the following basis: IF
Using SQL Server I have the following tables/ data CUS_VISIT Cus_ID Cus_Vis_ID 1 01
I have the following two tables in my mysql database. Table Name: groups id
I have the following two tables Groups Id (int) People Id (int) GroupId (int,
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3

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.