0

I have 2 tables that have similar data and I need to compare the values of one table with the other where the dates are equal.

Example tables:

CREATE TABLE [data].[ProfileReconciliation] (
[ProfileName] varchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[OriginalFeedDate] datetime NOT NULL ,
[AggregationDate] datetime NOT NULL ,
[TotalDescription] varchar(200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[TotalSalesAmount] decimal(18,2) NOT NULL ,
[LoadKey] int NULL 
)
ON [DATA]
GO

CREATE TABLE [data].[Aggregation] (
[ColumnName] varchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[dataDate] date,
[profileDate] datetime  ,
[aggregationType] varchar(200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[dataValue] decimal(18,2) NOT NULL ,

)
ON [DATA]
GO

So, the dataValue in the Aggregation table needs to be compared to the TotalSalesAmount in the ProfileReconciliation table. The dataDate in the Aggregation table matches up with the AggregationDate in the ProfileReconciliation table. The ProfileReconciliation table only contains data from a daily feed, so we only need to compare the dates that are in the ProfileReconciliation with the Aggregation table. I need to indicate weather the values of each table are equal and if not I need to show a flag indicating so. I started by selecting the data from the Aggregation table where the dataDate was in AggregationDate. I obviously have to cast the AggregationDate. I just have no clue where to head next or what the best approach to this is.

Example Data: From ProfileReconciliation table

OPDCost 2012-10-08 17:43:51.000 2012-10-07 00:00:00.000 SUM(Total_Net_Cost_Amt) 21323923.00 5307

OPDS    2012-10-08 17:43:51.000 2012-10-07 00:00:00.000 SUM(Total_Net_Sales_Amt)    70753228.00 5307

From Aggregation table:

SPVData.fact.DayStoreProdCost   2012-10-07  2012-10-09 09:06:20.9167944     20970788.131400 
SPVData.fact.DayStoreProdExtra   2012-10-07     2012-10-09 09:03:19.1558724     70642458.910000 

So as you can see the amounts do not match up so we wwould want teh results to look like this:

**ProfileName  Date                      ProfileAmount AggregationAmount   Equal**
OPDCost      2012-10-07 00:00:00.000   21323923.00   20970788.13         0
OPDS         2012-10-07 00:00:00.000   70753228.00   70642458.91         0
4

1 回答 1

1
Select p.ProfileName, p.AggregationDate,
p.TotalSalesAmount as ProfileAmount, a.DataValue as AggregationAmount,
Case When p.SalesAmount <> a.DataValue Then 1
else 0
end as Equal
From Aggregation a 
Inner Join ProfileReconcilation p On Convert(Date,p.AggregationDate) = a.DataDate

If I've understood correctly, which I'm not sure I have...

Not sure the Convert is necessary either, from your example looks like your DateTime has a data in it anyway.

于 2012-10-09T21:01:37.543 回答