我有兩個表,其中所有列都是相同的。以下是包含數據的腳本。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
顯示列名稱。
首先Thnig首先儘量避免任何品牌,實體的名字在你的例子..喜歡在這裏你已經使用「Airtel公司」,「商Aircel」 – 2014-09-30 10:56:14