我有两个表,其中所有列都相同。下面是带有数据的脚本。
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 :(得分: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
...