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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:47:23+00:00 2026-06-12T21:47:23+00:00

For a SQL Server datawarehouse, I need to match 2 tables containing roughly the

  • 0

For a SQL Server datawarehouse, I need to match 2 tables containing roughly the same data.

There is obviously more to it than this, so redefining the task is not an option 🙂

Given 2 tables, A and B

Table A:

id  | fid | type
-------------------
100 | 1   | cookies
110 | 1   | muffins
120 | 1   | muffins

Table B:

id   | fid | type
--------------------
a220 | 1   | muffins
b220 | 1   | muffins

When merged (apply secret IT here – SQL), it should become

A_B:

A_id | B_id | fid | type
---------------------------
100  | NULL | 1   | cookies
110  | a220 | 1   | muffins
120  | b220 | 1   | muffins

Any solution using T-SQL is preferred, performance is not an issue. If SSIS is a simpler option, I can live with that.


Here is a script for creating a test environment for you to play around with.

/****** Object:  Table [dbo].[B]    ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[B](
    [id] [varchar](10) NULL,
    [fid] [int] NULL,
    [type] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[B] ([id], [fid], [type]) VALUES (N'a220', 1, N'muffins')
INSERT [dbo].[B] ([id], [fid], [type]) VALUES (N'b220', 1, N'muffins')
/****** Object:  Table [dbo].[A]    ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[A](
    [id] [varchar](10) NULL,
    [fid] [int] NULL,
    [type] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[A] ([id], [fid], [type]) VALUES (N'100', 1, N'cookies')
INSERT [dbo].[A] ([id], [fid], [type]) VALUES (N'110', 1, N'muffins')
INSERT [dbo].[A] ([id], [fid], [type]) VALUES (N'120', 1, N'muffins')
  • 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-12T21:47:24+00:00Added an answer on June 12, 2026 at 9:47 pm

    Assuming you want to match on type and the order of the IDs…

    select a.id, b.id, ISNULL(a.fid,b.fid) fid, ISNULL(a.type,b.type) type
    from
        (select *, ROW_NUMBER() over (partition by type order by id) rn from a ) a
            full outer join
        (select *, ROW_NUMBER() over (partition by type order by id) rn from b ) b
            on a.rn=b.rn
            and a.type = b.type
    order by a.id       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a Data Warehouse for an existing SQL Server database. I
SQL Server 2008 Two tables: Table A has following data: RowA RowB RowC RowD
SQL Server 2000 Using More than 20 views, all the views are inner join
In Scaling Up Your Data Warehouse with SQL Server 2008 R2 , the author
SQL Server provides the type [rowguid]. I like to use this as unique primary
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers
I'm planning a data warehouse migration to SQL Server 2008, and trying to think
In a data warehouse, are there disadvantages to creating clustered indexes on fact tables?
I'm doing "Multidimensional Project" with MS SQL Server 2012 (Server Data Tools - Visual
SQL Server 2008. Is it possible to create Change Data Capture (or Change Tracking)

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.