我有两个不同的表,表1和表2.
Table 1
-----------
Resource
Start Date
End date
Total Hours
Table 2
------------
Resource
Reource type
Total Hours
在这里,我必须根据资源匹配每个表的总小时数, 然后必须找到不匹配的数据,并找出与不匹配数据的值的差异。
答案 0 :(得分:0)
SELECT [Table 1].[Resource],
([Table 1].[Total Hours] - [Table 2].[Total Hours]) As Difference
FROM [Table 1]
INNER JOIN [Table 2]
ON [Table 1].[Resource] = [Table 2].[Resource]
WHERE [Table 1].[Total Hours] <> [Table 2].[Total Hours]
如果我理解正确,这将为您提供总小时数及其所属资源的差异。括号是必要的,因为列名和表名都很糟糕。
答案 1 :(得分:0)
也许[Resource]不是Table1中的唯一键。 所以我们需要一些group by子句:
SELECT Resource,SUM([Total Hours]) As SumHours1
INTO #SumHours1
FROM [Table 1]
GROUP BY [Resource];
SELECT Resource,SUM([Total Hours]) As SumHours2
INTO #SumHours2
FROM [Table 2]
GROUP BY [Resource];
SELECT Resource,SumHours1,SumHours2
FROM #SumHours1 LEFT OUTER JOIN #SumHours2
ON #SumHours1.Resource=#SumHours2.Resource