Merging tables with duplicate data

元气小坏坏 提交于 2019-12-07 04:54:49

问题


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:


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       


来源:https://stackoverflow.com/questions/12910287/merging-tables-with-duplicate-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!