2014-09-30 60 views
-1

我有兩個表,其中所有列都是相同的。以下是包含數據的腳本。SQL DATA COMPARE

CREATE TABLE [dbo].[Tbl_Prices_2] 
(
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [State] [varchar](50) NULL, 
    [MajorRegion] [varchar](50) NULL, 
    [ProductGroup] [varchar](50) NULL, 
    [PriceToRetailer] [decimal](18, 2) NULL, 
    [PriceToAgent] [decimal](18, 2) NULL, 
    [PriceToDistributor] [decimal](18, 2) NULL, 
    [PriceToAdmin] [decimal](18, 2) NULL, 

    CONSTRAINT [PK_Tbl_Prices_2] 
     PRIMARY KEY CLUSTERED ([ID] ASC) 
) 
GO 

SET IDENTITY_INSERT [dbo].[Tbl_Prices_2] ON 

INSERT [dbo].[Tbl_Prices_2] ([ID], [State], [MajorRegion], [ProductGroup], [PriceToRetailer], [PriceToAgent], [PriceToDistributor], [PriceToAdmin]) VALUES (1, N'Assam', N'ABC', N'AIRTEL', CAST(1.00 AS Decimal(18, 2)), CAST(3.00 AS Decimal(18, 2)), CAST(2.00 AS Decimal(18, 2)), CAST(3.00 AS Decimal(18, 2))) 
INSERT [dbo].[Tbl_Prices_2] ([ID], [State], [MajorRegion], [ProductGroup], [PriceToRetailer], [PriceToAgent], [PriceToDistributor], [PriceToAdmin]) VALUES (2, N'Bihar', N'XYZ', N'IDEA', CAST(1.00 AS Decimal(18, 2)), CAST(4.00 AS Decimal(18, 2)), CAST(2.00 AS Decimal(18, 2)), CAST(3.00 AS Decimal(18, 2))) 
INSERT [dbo].[Tbl_Prices_2] ([ID], [State], [MajorRegion], [ProductGroup], [PriceToRetailer], [PriceToAgent], [PriceToDistributor], [PriceToAdmin]) VALUES (3, N'Goa', N'PQR', N'AIRCEL', CAST(2.00 AS Decimal(18, 2)), CAST(5.00 AS Decimal(18, 2)), CAST(4.00 AS Decimal(18, 2)), CAST(6.00 AS Decimal(18, 2))) 
SET IDENTITY_INSERT [dbo].[Tbl_Prices_2] OFF 

CREATE TABLE [dbo].[Tbl_Prices_1] 
(
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [State] [varchar](50) NULL, 
    [MajorRegion] [varchar](50) NULL, 
    [ProductGroup] [varchar](50) NULL, 
    [PriceToRetailer] [decimal](18, 2) NULL, 
    [PriceToAgent] [decimal](18, 2) NULL, 
    [PriceToDistributor] [decimal](18, 2) NULL, 
    [PriceToAdmin] [decimal](18, 2) NULL, 

    CONSTRAINT [PK_Tbl_Prices_1] 
     PRIMARY KEY CLUSTERED ([ID] ASC) 
) 

SET IDENTITY_INSERT [dbo].[Tbl_Prices_1] ON 
INSERT [dbo].[Tbl_Prices_1] ([ID], [State], [MajorRegion], [ProductGroup], [PriceToRetailer], [PriceToAgent], [PriceToDistributor], [PriceToAdmin]) VALUES (1, N'Assam', N'ABC', N'AIRTEL', CAST(1.00 AS Decimal(18, 2)), CAST(1.00 AS Decimal(18, 2)), CAST(2.00 AS Decimal(18, 2)), CAST(3.00 AS Decimal(18, 2))) 
INSERT [dbo].[Tbl_Prices_1] ([ID], [State], [MajorRegion], [ProductGroup], [PriceToRetailer], [PriceToAgent], [PriceToDistributor], [PriceToAdmin]) VALUES (2, N'Bihar', N'XYZ', N'IDEA', CAST(1.00 AS Decimal(18, 2)), CAST(1.00 AS Decimal(18, 2)), CAST(2.00 AS Decimal(18, 2)), CAST(3.00 AS Decimal(18, 2))) 
INSERT [dbo].[Tbl_Prices_1] ([ID], [State], [MajorRegion], [ProductGroup], [PriceToRetailer], [PriceToAgent], [PriceToDistributor], [PriceToAdmin]) VALUES (3, N'Goa', N'PQR', N'AIRCEL', CAST(6.00 AS Decimal(18, 2)), CAST(3.00 AS Decimal(18, 2)), CAST(4.00 AS Decimal(18, 2)), CAST(6.00 AS Decimal(18, 2))) 
SET IDENTITY_INSERT [dbo].[Tbl_Prices_1] OFF 

我需要比較每個州和MajorRegion明智的價格。我需要下面的輸出:

TableName  ID State MajorRegion ProductGroup Col_Difference Value 
------------------------------------------------------------------------------- 
Tbl_Prices_1 1  Assam ABC   AIRTEL   PriceToAgent  1 
Tbl_Prices_2 1  Assam ABC   AIRTEL   PriceToAgent  3 
Tbl_Prices_1 1  Goa  PQR   AIRCEL   PriceToRetailer 2 
Tbl_Prices_2 1  Goa  PQR   AIRCEL   PriceToRetailer 3 

這裏Col_Difference顯示列名稱。

+0

首先Thnig首先儘量避免任何品牌,實體的名字在你的例子..喜歡在這裏你已經使用「Airtel公司」,「商Aircel」 – 2014-09-30 10:56:14

回答

0
select 'Tbl_Prices_1' as [tableName], Tbl_Prices_1.ID, 
     'PriceToAgent' as [Col_Difference], Tbl_Prices_1.PriceToAgent 
    from Tbl_Prices_1 
    join Tbl_Prices_2 
    on Tbl_Prices_2.ID = Tbl_Prices_1.ID 
    and Tbl_Prices_2.PriceToAgent <> Tbl_Prices_1.PriceToAgent 
union 
select 'Tbl_Prices_2' as [tableName], Tbl_Prices_2.ID, 
     'PriceToAgent' as [Col_Difference], Tbl_Prices_2.PriceToAgent 
    from Tbl_Prices_2 
    join Tbl_Prices_1 
    on Tbl_Prices_2.ID = Tbl_Prices_1.ID 
    and Tbl_Prices_2.PriceToAgent <> Tbl_Prices_1.PriceToAgent 

...

+0

謝謝,假設我有100在'Tbl_Prices_1'和'Tbl_Prices_2'表中,我需要聯合100次? – 2014-09-30 13:00:31

+0

Boo hoo。這應該需要10分鐘。 – Paparazzi 2014-09-30 13:01:56