跨多个表的 SUM 和 JOIN 才能正常工作。返回错误结果
SUM and JOIN across multiple tables to work properly. Wrong results returned
有5个table正在被查询。
- 分配(table 将客户分配给审计员)
- AssignmentCarriers(运营商列表及其设置。链接到分配)
- 审计员(人)
- 客户
- 索赔(审计员代表客户输入的索赔)
- 条目(每个声明可以有多个条目。这是我们从中获取 $$ 的地方)
场景如下。管理层可以分配一个客户给一个审计员。该审计师可以为他的客户开立 索赔 以尝试赚取 $$。
我必须找出审计员分配给了哪些客户,他在特定时间段内提出了哪些索赔,以及总共退回了多少 $$。这是我的代码。我会把它粘贴两次,这样你就可以看到它看起来像未评论和评论。然后我会展示我目前的结果,希望有人能帮助我,因为我似乎无法弄清楚这里到底发生了什么。
中断代码
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','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 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,
code,-- 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','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, -- 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','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 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
当我 运行 直接在 claims/entries table 上使用 WHERE auditorID=101 进行查询,然后针对指定的日期和代码进行查询时,确实 JanPC20 = 0 但 JanRC20 = 2669.0794.
实际上只返回了 1 条记录,“2669.0794”是 refundDue 列中的金额。这里发生了什么?我期待着我能得到的任何帮助。谢谢!
运行 以下内容,看看什么是三倍:
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','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 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
这将 return 3 行。所有 3 行的 return 相同 ID 的 ID 列不是问题。具有不同值的 ID 列是问题所在。这些 ID 来自的 table 是具有多个值的 table。您可能需要进一步合并 table 以获得您想要的值。
有5个table正在被查询。
- 分配(table 将客户分配给审计员)
- AssignmentCarriers(运营商列表及其设置。链接到分配)
- 审计员(人)
- 客户
- 索赔(审计员代表客户输入的索赔)
- 条目(每个声明可以有多个条目。这是我们从中获取 $$ 的地方)
场景如下。管理层可以分配一个客户给一个审计员。该审计师可以为他的客户开立 索赔 以尝试赚取 $$。
我必须找出审计员分配给了哪些客户,他在特定时间段内提出了哪些索赔,以及总共退回了多少 $$。这是我的代码。我会把它粘贴两次,这样你就可以看到它看起来像未评论和评论。然后我会展示我目前的结果,希望有人能帮助我,因为我似乎无法弄清楚这里到底发生了什么。
中断代码
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','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 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,
code,-- 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','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, -- 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','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 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
当我 运行 直接在 claims/entries table 上使用 WHERE auditorID=101 进行查询,然后针对指定的日期和代码进行查询时,确实 JanPC20 = 0 但 JanRC20 = 2669.0794.
实际上只返回了 1 条记录,“2669.0794”是 refundDue 列中的金额。这里发生了什么?我期待着我能得到的任何帮助。谢谢!
运行 以下内容,看看什么是三倍:
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','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 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
这将 return 3 行。所有 3 行的 return 相同 ID 的 ID 列不是问题。具有不同值的 ID 列是问题所在。这些 ID 来自的 table 是具有多个值的 table。您可能需要进一步合并 table 以获得您想要的值。