跨多个表的SUM和JOIN可以正常工作返回错误的结果

问题描述

有5个表正在查询

  1. 分配(将客户分配给审核员的表)
  2. AssignmentCarriers(运营商列表及其设置。链接到分配)
  3. 审核员(人)
  4. 客户
  5. 索赔(由审计师代表客户提出的索赔)
  6. 条目(每个声明可以有多个条目。这是我们从中获取$$的位置)

这是场景。管理层可以指派 客户审计师。该审核员可以为客户打开索赔,以尝试赚取$$。

我必须找出指定审核员的客户以及在特定时间段内他提出的索赔要求以及退还了总金额$$。这是我的代码。我将其粘贴两次,以便您可以看到未评论评论内容。然后,我将显示当前的结果,并希望有人能帮助我,因为我似乎无法弄清楚这里到底发生了什么。

代码中断

SELECT disTINCT 
  a.clientID,code,SUM(case when cl.dateon >='1/1/2020' AND cl.dateon < '1/3/2020' AND entries.errorCode NOT IN('DP','RB','DN','WP','WA','CE','RC','SI','CI','PE','OV') AND status='closed' AND cl.client=code then entries.refundDue else 0.0 end) as JanRC20,SUM(case when cl.dateon >='1/1/2020' AND cl.dateon < '1/3/2020' AND entries.errorCode IN('DP','OV') AND status='closed' AND cl.client=code then entries.refundDue else 0.0 end) as JanPC20

FROM assignments a 

INNER JOIN clients c ON c.clientID=a.clientID 
INNER JOIN AssignmentCarriers ac ON ac.acID=a.acID 
INNER JOIN claims cl ON cl.auditorID=a.auditorID
INNER JOIN entries ON entries.rID=cl.rID


WHERE a.auditorID=101 AND isAssignment='True' AND active='True' AND pos=1

GROUP BY a.clientID,code

ORDER BY code

注释代码

SELECT disTINCT
  a.clientID,-- being an older database,the uniqueID here is the code,not clientID
  SUM(case when cl.dateon >='1/1/2020' AND cl.dateon < '1/3/2020' AND entries.errorCode NOT IN('DP',-- this is supposed to SUM up the "refundDue" for the specified time period for claims that have a status of closed and does not have a specific errorCode.
  SUM(case when cl.dateon >='1/1/2020' AND cl.dateon < '1/3/2020' AND entries.errorCode IN('DP','OV') AND status='closed' AND cl.client=code then entries.refundDue else 0.0 end) as JanPC20 -- same as the prevIoUs but this includes the specified errorCodes

FROM assignments a 

INNER JOIN clients c ON c.clientID=a.clientID -- this brings in the code from the clients table and whether it's active or not (bool)
INNER JOIN AssignmentCarriers ac ON ac.acID=a.acID -- for checking if isAssignments='True'
INNER JOIN claims cl ON cl.auditorID=a.auditorID -- brings in claims table
INNER JOIN entries ON entries.rID=cl.rID -- brings in entries table


WHERE a.auditorID=101 AND isAssignment='True' AND active='True' AND pos=1 -- only return results where a specified auditor (101) is assigned

GROUP BY a.clientID,code

ORDER BY code

我希望这是有道理的。我觉得我已经很近了,但是没有用。当我运行代码时,我确实获得了所有分配给客户的审核员的列表。很好剩下的是$$金额。因此,专注于分配了该审核员的1个客户,结果如下:

clientID.  code.   JanRC20.   JanPC20. 
678        INCM   8007.2382    0.0000

当我直接在WHERE auditerID = 101的声明/条目表上运行查询,然后针对指定的日期和代码运行时,确实JanPC20 = 0但JanRC20 = 2669.0794。

实际上仅返回了1条记录,而“ 2669.0794”是refreaseDue列中的金额。这里发生了什么?我期待获得任何帮助。谢谢!

解决方法

运行以下命令,然后查看三倍的内容:

SELECT DISTINCT 
  a.clientID,code,case when cl.dateon >='1/1/2020' AND cl.dateon < '1/3/2020' AND entries.errorCode NOT IN('DP','RB','DN','WP','WA','CE','RC','SI','CI','PE','OV') AND status='closed' AND cl.client=code then entries.refundDue else 0.0 end as JanRC20,case when cl.dateon >='1/1/2020' AND cl.dateon < '1/3/2020' AND entries.errorCode IN('DP','OV') AND status='closed' AND cl.client=code then entries.refundDue else 0.0 end as JanPC20,a.assignmentID,c.clientID,ac.acID,cl.claimID,a.auditorID,entries.rID

FROM assignments a 

INNER JOIN clients c ON c.clientID=a.clientID 
INNER JOIN AssignmentCarriers ac ON ac.acID=a.acID 
INNER JOIN claims cl ON cl.auditorID=a.auditorID
INNER JOIN entries ON entries.rID=cl.rID


WHERE a.auditorID=101 AND isAssignment='True' AND active='True' AND pos=1

--GROUP BY a.clientID,code

ORDER BY code

这将返回3行。对于所有3行返回相同ID的ID列不是问题。问题是ID列的值不同。这些ID来自的表是具有多个值的表。您可能需要对该表进行进一步合并以获取所需的值。