比较两个表中的数据

发布于 2024-10-21 09:27:52 字数 536 浏览 7 评论 0原文

我有两个表需要比较以确保值匹配。对于上下文,一个是员工的时间表,另一个是保存其请求的表。每个都有一个时间表代码和小时数。我的计划是通过摘要进行比较,看看它们是否匹配。然而,我每次都无法让我的逻辑完美运行。如果我不发布我的代码(无论如何它很快就会变得一团糟),你会如何处理这个问题?比较需要能够以编程方式运行并最终返回真/假。

这可以是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一片旧的回忆 2024-10-28 09:27:52

这将比较 A 和 B 中每个 ID 值的总小时数,并且仅返回 B 之和不等于 A 的记录。它将不匹配的 B 值视为零。

SELECT A.id, A.hours, SUM(COALESCE(B.Hours,0))
FROM A
LEFT OUTER JOIN B
  ON B.ID = A.ID
WHERE 1=1
  AND A.id = B.id
GROUP BY A.id
HAVING A.Hours != SUM(COALESCE(B.Hours,0))

Cheers,
Daniel

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.

SELECT A.id, A.hours, SUM(COALESCE(B.Hours,0))
FROM A
LEFT OUTER JOIN B
  ON B.ID = A.ID
WHERE 1=1
  AND A.id = B.id
GROUP BY A.id
HAVING A.Hours != SUM(COALESCE(B.Hours,0))

Cheers,
Daniel
九歌凝 2024-10-28 09:27:52

这应该给出所请求的结果:

with w1 ( wid, sumHrs )
 as ( SELECT id, sum(hours)
        FROM A   a1
        GROUP BY id ),
     w2 ( wid, sumHrs )
 as ( SELECT id, sum(hours)
        FROM B  b1
        GROUP BY id )
 select '1', w1.wid from w1 INNER JOIN w2 
                                on w1.wid = w2.wid
                      where w1.sumHrs <> w2.sumHrs 
union
 select '2', w1.wid from w1 EXCEPTION JOIN w2 
                                on w1.wid = w2.wid
union
 select '3', w2.wid from w2 EXCEPTION JOIN w1 
                                on w2.wid = w1.wid 

由于对我拥有的接近但不准确的示例进行了调整和简化,可能需要进行一些小的细节更改。不过,所有基本概念都应该很清楚。

CTE 提供了几个摘要“观点”,供在完整声明中使用。 INNER JOIN 给出了具有不匹配 HOURS 的匹配 ID。第一个 EXCEPTION JOIN 给出第一个表中与第二个表中没有匹配 ID 的行,第二个 EXCEPTION JOIN 给出在另一个方向上不匹配的行。 UNION 将所有结果收集在一起。

This should give the requested results:

with w1 ( wid, sumHrs )
 as ( SELECT id, sum(hours)
        FROM A   a1
        GROUP BY id ),
     w2 ( wid, sumHrs )
 as ( SELECT id, sum(hours)
        FROM B  b1
        GROUP BY id )
 select '1', w1.wid from w1 INNER JOIN w2 
                                on w1.wid = w2.wid
                      where w1.sumHrs <> w2.sumHrs 
union
 select '2', w1.wid from w1 EXCEPTION JOIN w2 
                                on w1.wid = w2.wid
union
 select '3', w2.wid from w2 EXCEPTION JOIN w1 
                                on w2.wid = w1.wid 

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 first EXCEPTION JOIN gives rows from the first table with no matching ID in the second table, and the second EXCEPTION JOIN gives rows that aren't matched in the other direction. And the UNIONs collect all results together.

有深☉意 2024-10-28 09:27:52

一般来说,如果合并两个表,并且生成的表的计数与各个表的计数相同,则可以说它们具有相同的值。

我不确定如何申请您的情况,但也许可以。

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.

鯉魚旗 2024-10-28 09:27:52

最简单的解决方案显示冲突的行:

CREATE TABLE requests ( employees_id integer, hours decimal );
CREATE TABLE hours ( employees_id integer, hours decimal );

SELECT * FROM requests, hours WHERE requests.employees_id = hours.employees_id AND requests.hours != hours.hours;

但是,如果例如一个 2h 请求需要匹配两个 1h 标记,则它不起作用。

The simplest solution show lines that are in conflict:

CREATE TABLE requests ( employees_id integer, hours decimal );
CREATE TABLE hours ( employees_id integer, hours decimal );

SELECT * FROM requests, hours WHERE requests.employees_id = hours.employees_id AND requests.hours != hours.hours;

But it doesn't work if e.g. one 2h request needs to match two 1h markings.

我最近做了类似的事情 - 这是我的笔记...结果集显示了一个表中存在的值和行的差异,但另一个表中没有。

步骤1:创建包含要比较的行的临时表。

步骤2:使用非聚合列作为匹配标准对临时表执行FULL OUTER JOIN。

DECLARE @Ta TABLE (PubA varchar(255), CampA varchar(255), RevA money)   

INSERT INTO @Ta (PubA, CampA, RevA)                     
    SELECT Publisher, [Campaign Name], SUM([Revenue USD])               
    FROM D1.dbo.Stats               
    GROUP BY Publisher, [Campaign Name]

DECLARE @Tb TABLE (PubB varchar(255), CampB varchar(255), RevB money)   

INSERT INTO @Tb (PubB, CampB, RevB)                     
    SELECT Publisher, [Campaign Name], SUM([Revenue USD])               
    FROM D2.dbo.Stats                   
    GROUP BY Publisher, [Campaign Name]

SELECT PubA, CampA, RevA, PubB, CampB, RevB, RevA-RevB DiffRev  
FROM @Ta                        
FULL OUTER JOIN @Tb                     
ON PubA=PubB and CampA=CampB                        
ORDER BY DiffRev DESC                       
GO  

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.

DECLARE @Ta TABLE (PubA varchar(255), CampA varchar(255), RevA money)   

INSERT INTO @Ta (PubA, CampA, RevA)                     
    SELECT Publisher, [Campaign Name], SUM([Revenue USD])               
    FROM D1.dbo.Stats               
    GROUP BY Publisher, [Campaign Name]

DECLARE @Tb TABLE (PubB varchar(255), CampB varchar(255), RevB money)   

INSERT INTO @Tb (PubB, CampB, RevB)                     
    SELECT Publisher, [Campaign Name], SUM([Revenue USD])               
    FROM D2.dbo.Stats                   
    GROUP BY Publisher, [Campaign Name]

SELECT PubA, CampA, RevA, PubB, CampB, RevB, RevA-RevB DiffRev  
FROM @Ta                        
FULL OUTER JOIN @Tb                     
ON PubA=PubB and CampA=CampB                        
ORDER BY DiffRev DESC                       
GO  
酒几许 2024-10-28 09:27:52

我将两个 SQL 结果放入视图中,然后根据许多人的反馈已经提出了这个。我认为这可以创建到它​​自己的存储过程中。

SELECT SUM ( ERROR_COUNT ) AS TOTAL_ERRORS INTO NUM_ERRORS FROM (
SELECT COUNT ( * ) AS ERROR_COUNT
FROM MPRLIB . V_TSHOURSUMM A EXCEPTION JOIN MPRLIB . V_REQHOURSUMM B
ON A . EM_NUMBER = B . EM_NUMBER AND A . TIMESHEET_CODE = B . TIMESHEET_CODE AND A.HOURS_SUMMARY = B . HOURS_SUMMARY
WHERE A . EM_NUMBER = EMPLOYEE_ID OR B . EM_NUMBER = EMPLOYEE_ID
UNION
SELECT COUNT ( * ) AS ERROR_COUNT
FROM MPRLIB . V_REQHOURSUMM A EXCEPTION JOIN MPRLIB . V_TSHOURSUMM B
ON A . EM_NUMBER = B . EM_NUMBER AND A . TIMESHEET_CODE = B . TIMESHEET_CODE AND A . HOURS_SUMMARY = B . HOURS_SUMMARY
WHERE A . EM_NUMBER = EMPLOYEE_ID OR B . EM_NUMBER = EMPLOYEE_ID ) TABLE

这似乎有效,但似乎……太过分了。想法?有更好的办法吗?

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.

SELECT SUM ( ERROR_COUNT ) AS TOTAL_ERRORS INTO NUM_ERRORS FROM (
SELECT COUNT ( * ) AS ERROR_COUNT
FROM MPRLIB . V_TSHOURSUMM A EXCEPTION JOIN MPRLIB . V_REQHOURSUMM B
ON A . EM_NUMBER = B . EM_NUMBER AND A . TIMESHEET_CODE = B . TIMESHEET_CODE AND A.HOURS_SUMMARY = B . HOURS_SUMMARY
WHERE A . EM_NUMBER = EMPLOYEE_ID OR B . EM_NUMBER = EMPLOYEE_ID
UNION
SELECT COUNT ( * ) AS ERROR_COUNT
FROM MPRLIB . V_REQHOURSUMM A EXCEPTION JOIN MPRLIB . V_TSHOURSUMM B
ON A . EM_NUMBER = B . EM_NUMBER AND A . TIMESHEET_CODE = B . TIMESHEET_CODE AND A . HOURS_SUMMARY = B . HOURS_SUMMARY
WHERE A . EM_NUMBER = EMPLOYEE_ID OR B . EM_NUMBER = EMPLOYEE_ID ) TABLE

It seems to work, but seems... excessive. Thoughts? Is there a better way?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文