Sql server SQL数据比较
我有两个表,其中所有列都相同。下面是包含数据的脚本Sql server SQL数据比较,sql-server,sql-server-2008,tsql,Sql Server,Sql Server 2008,Tsql,我有两个表,其中所有列都相同。下面是包含数据的脚本 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) N
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
我需要比较每个州和主要地区的价格。我需要以下输出:
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显示了列名
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
…首先,在你的例子中,尽量避免使用任何品牌、实体的名称。。就像这里你用的“Airtel”,“Aircel”谢谢,假设我在“Tbl_Prices_1”和“Tbl_Prices_2”表中有100个克隆,那么我需要联合100次?呜呜。这应该需要10分钟。