比较两个表中的数据
我有两个表需要比较以确保值匹配。对于上下文,一个是员工的时间表,另一个是保存其请求的表。每个都有一个时间表代码和小时数。我的计划是通过摘要进行比较,看看它们是否匹配。然而,我每次都无法让我的逻辑完美运行。如果我不发布我的代码(无论如何它很快就会变得一团糟),你会如何处理这个问题?比较需要能够以编程方式运行并最终返回真/假。
这可以是 RPG 解决方案或 SQL 解决方案。
这就是我需要确定的是真的。
Table 1
02 1.5
04 16.0
Table 2
02 1.5
04 16.0
问题是何时
Table 1
02 1.5
Table 2
02 1.5
04 16.0
或何时
Table 1
02 1.5
04 16.0
Table 2
02 1.5
或更何时
Table 1
02 1.5
04 16.0
Table 2
I have two tables that I need to compare to make sure the values match. For context, one is the employee's time sheet and the other is a table to holds their requests. Each has a time sheet code and a number of hours. My plan was to compare by summary to see if they match. However, I am having problems getting my logic to work perfectly every time. Without me posting my code (it is becoming a mess quickly anyway), how would you approach this? The comparison needs to be able to be run programmatically and in the end return a true/false.
This can be an RPG solution or a SQL solution.
This is what I need to make sure is true.
Table 1
02 1.5
04 16.0
Table 2
02 1.5
04 16.0
The problem is when
Table 1
02 1.5
Table 2
02 1.5
04 16.0
or when
Table 1
02 1.5
04 16.0
Table 2
02 1.5
or more so when
Table 1
02 1.5
04 16.0
Table 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这将比较 A 和 B 中每个 ID 值的总小时数,并且仅返回 B 之和不等于 A 的记录。它将不匹配的 B 值视为零。
This will compare the total hours in A and B for each ID value, and only return records there the sum of B does not equal A. It treats unmatched values of B as zero.
这应该给出所请求的结果:
由于对我拥有的接近但不准确的示例进行了调整和简化,可能需要进行一些小的细节更改。不过,所有基本概念都应该很清楚。
CTE 提供了几个摘要“观点”,供在完整声明中使用。
INNER JOIN
给出了具有不匹配 HOURS 的匹配 ID。第一个EXCEPTION JOIN
给出第一个表中与第二个表中没有匹配 ID 的行,第二个EXCEPTION JOIN
给出在另一个方向上不匹配的行。UNION
将所有结果收集在一起。This should give the requested results:
There might be minor detail changes needed due to adapting and simplifying from a sample I have that's close but not exact. All of the basic concepts should be clear, though.
The CTE gives a couple summary "views" for use in the full statement. The
INNER JOIN
gives matching IDs with mismatched HOURS. The firstEXCEPTION JOIN
gives rows from the first table with no matching ID in the second table, and the secondEXCEPTION JOIN
gives rows that aren't matched in the other direction. And theUNIONs
collect all results together.一般来说,如果合并两个表,并且生成的表的计数与各个表的计数相同,则可以说它们具有相同的值。
我不确定如何申请您的情况,但也许可以。
In general, if you merge two tables, and the count of the resulting table is the same as the count of the individual tables, you can say that they have the same values.
I am not sure how to apply in your case, but maybe you can.
最简单的解决方案显示冲突的行:
但是,如果例如一个 2h 请求需要匹配两个 1h 标记,则它不起作用。
The simplest solution show lines that are in conflict:
But it doesn't work if e.g. one 2h request needs to match two 1h markings.
我最近做了类似的事情 - 这是我的笔记...结果集显示了一个表中存在的值和行的差异,但另一个表中没有。
步骤1:创建包含要比较的行的临时表。
步骤2:使用非聚合列作为匹配标准对临时表执行FULL OUTER JOIN。
I did something like this recently - here are my notes... the result set shows differences in values plus rows that are present in one table but not the other.
Step1: Create temporary tables containing the rows you want to compare.
Step2: Perform a FULL OUTER JOIN on the temp tables using the non aggregate columns as matching criterion.
我将两个 SQL 结果放入视图中,然后根据许多人的反馈已经提出了这个。我认为这可以创建到它自己的存储过程中。
这似乎有效,但似乎……太过分了。想法?有更好的办法吗?
I put my two SQL results into views then based on many peoples feedback already came up with this. I think this could be created into it's own stored procedure.
It seems to work, but seems... excessive. Thoughts? Is there a better way?