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')
Assuming you want to match on type and the order of the IDs…