Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging tables with duplicate data

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')
like image 577
Peter Jensen Avatar asked Oct 07 '22 07:10

Peter Jensen


1 Answers

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       
like image 124
podiluska Avatar answered Oct 16 '22 14:10

podiluska