我有两个表,其中所有列都是相同的。以下是包含数据的脚本。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