支付金额大于支付总额

Posted

技术标签:

【中文标题】支付金额大于支付总额【英文标题】:Paid Amount Greater than Total Amount Paid 【发布时间】:2013-08-07 17:23:16 【问题描述】:

我有一个为客户维护的 Access 数据库。

我有 4 张桌子。声明、资格、药房和代码。

我使用的主键是 PHID + SID = MemberID,我链接到每个表,然后代码表仅用于描述。请参阅下面的查询以获得更好的可视化...

查询 1:Member_Claims_Query

SELECT
Eligibility.GROUPID, 
Eligibility.PHID & '-' & Eligibility.SID AS MemberID, 
[Eligibility].[DOB] AS DOB, 
Eligibility.GENDER, 
Eligibility.RELATIONSHIP_CODE, 
MaxDiagDollars.HighestDiagPaid/SUM(Claims.PAID_AMT) AS ['%'],
MaxDiagDollars.HighestDiagPaid/SUM(Claims.PAID_AMT) as 'Percent',
ROUND(SUM(Claims.PAID_AMT)) AS TOTALPAID,
ROUND(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2011',Claims.PAID_AMT,0))) AS 2011TOTALPAID, 
ROUND(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2012',Claims.PAID_AMT,0))) AS 2012TOTALPAID, 
ROUND(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2013',Claims.PAID_AMT,0))) AS 2013TOTALPAID
FROM (Claims 
INNER JOIN Eligibility 
ON (Claims.[SID] = Eligibility.[SID]) AND (Claims.[PHID] = Eligibility.[PHID])) 
INNER JOIN (SELECT PHID, SID, MAX(TotalPaid) AS HighestDiagPaid 
FROM (SELECT [PHID], [SID], DIAG_CODE1, SUM(PAID_AMT) AS TotalPaid FROM Claims GROUP BY [PHID], [SID], [DIAG_CODE1])  AS [%$##@_Alias] GROUP BY PHID, SID)  AS MaxDiagDollars ON ( MaxDiagDollars.[PHID]=Eligibility.[PHID] ) AND ( MaxDiagDollars.[SID] = Eligibility.[SID] )
WHERE Eligibility.DOB < DateAdd( 'y', -2, DATE())
GROUP BY 
Eligibility.GROUPID, Eligibility.PHID & '-' & Eligibility.SID, [Eligibility].[DOB], Eligibility.GENDER, Eligibility.RELATIONSHIP_CODE, MaxDiagDollars.HighestDiagPaid
HAVING SUM(Claims.PAID_AMT)>10000 and  MaxDiagDollars.HighestDiagPaid/SUM(Claims.PAID_AMT) <= 0.80;

这个查询应该获取每个会员支付的总金额并给出一个总金额 PAid,然后每年爆发。

查询 2:Member_By_Diag

SELECT 
Eligibility.PHID & '-' & Eligibility.SID AS MemberID, 
Claims.Diag_Code1, 
ROUND(Sum(Claims.PAID_AMT)) AS TotalPaid, 
ROUND(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2011',Claims.PAID_AMT,0))) AS 2011TotalPaid, 
ROUND(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2012',Claims.PAID_AMT,0))) AS 2012TotalPaid, 
ROUND( Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2013',Claims.PAID_AMT,0))) AS 2013TotalPaid
FROM 
(Claims 
INNER JOIN Eligibility 
ON (Claims.[SID] = Eligibility.[SID]) AND (Claims.[PHID] = Eligibility.[PHID])) 
INNER JOIN Pharmacy 
ON (Eligibility.SID = Pharmacy.SID) AND (Eligibility.PHID = Pharmacy.PHID)
GROUP BY 
Eligibility.PHID & '-' & Eligibility.SID,  Claims.Diag_Code1
HAVING count( [Pharmacy].[NDC] ) >4 and count(IIF(Claims.REV_CODE= '450',1,0) ) > 1
ORDER BY Eligibility.PHID & '-' & Eligibility.SID;

第二个查询本质上应该获取每个成员的代码并通过诊断代码分解他们支付的金额。

查询 3:combined_query

SELECT *
FROM (Member_Claims_Query AS a INNER JOIN Member_by_Diag AS b ON a.MemberID=b.MemberID) INNER JOIN Codes AS c ON c.DxCode = b.Diag_Code1;

问题 我的客户给我发了一封电子邮件,说明 Member_By_Diag 查询中的总支付金额有时高于 Member_By_Claim 查询的总支付金额。但是它们的计算方式相同。

我打开数据库并编写了一个简单的查询来查看有多少记录返回,其中 b.Total_Paid (Member_By_Diag.Total_Paid) 大于 Member_Claims_Query.Total_Paid。

它返回了 262/1278 条记录。

SELECT * FROM Combined_Query WHERE b_TotalPaid > a_TotalPaid

这张照片准确地描述了我和我的客户所看到的。

如您所见。 a_TotalPaid > b_TotalPaid。但是,如果您查看我的查询,它们是否相同?这是按问题分组吗?还是加入问题?任何帮助将不胜感激。

【问题讨论】:

我害怕遗漏一些东西,不管怎样,我的意思是,它可能会影响结果。这里有一个逻辑的事件流,以及查询和结果。你还想要什么? 一目了然(你可以做更多的格式化),第一个查询(Member_Claims_Query,或a)有一个额外的INNER JOIN,可能会删除一些行(特别是INNER JOIN (SELECT PHID, SID, MAX(TotalPaid) AS HighestDiagPaid ...,这可以使b.TotalPaid &gt; a.TotalPaid. 优秀的观察肯 一个关键的观察是INNER JOIN 可以引入额外 记录以及消除一些记录。 【参考方案1】:

可能导致此问题的查询之间存在一些差异。罪魁祸首是您的 INNER JOIN 语句和 HAVING 语句。这些很容易排除会影响您的 TotalPaid 字段的记录。如果没有原始数据集,我可以告诉您的不多,但您可能希望运行这些查询并尝试删除和插入各种 INNER JOIN 和 HAVING 子句,以查看哪个正在删除导致您的总计不的记录相等。

【讨论】:

【参考方案2】:

感谢大家的回答...您没有完全回答问题,但是 Pharmacy 的内部联接导致了问题,当我添加了一个 count(* ) 我注意到它实际上使我的结果成倍增加。对于前。如果一个成员有 7 条索赔和 6 条药房记录,那么它会成倍增加,使其成为 42 条记录,使我的总支付非常高,而且它们与索赔本身无关……因此是最终问题。这是 Member_By_Diag 查询中的解决方案:

    SELECT Eligibility.PHID & '-' & Eligibility.SID AS MemberID, Claims.Diag_Code1, Round(Sum(Claims.PAID_AMT)) AS TotalPaid, Round(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2011',Claims.PAID_AMT,0))) AS 2011TotalPaid, Round(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2012',Claims.PAID_AMT,0))) AS 2012TotalPaid, Round(Sum(IIf(Format(Serv_Beg_Date,'yyyy')='2013',Claims.PAID_AMT,0))) AS 2013TotalPaid, Count(*) AS Expr1
FROM (Claims INNER JOIN Eligibility ON (Claims.[SID] = Eligibility.[SID]) AND (Claims.[PHID] = Eligibility.[PHID])) INNER JOIN ***(SELECT PHID, SID, COUNT(NDC) AS RXCount FROM Pharmacy GROUP BY PHID, SID ORDER BY PHID, SID)  AS Pharmacy*** ON (Eligibility.SID = Pharmacy.SID) AND (Eligibility.PHID = Pharmacy.PHID)
GROUP BY Eligibility.PHID & '-' & Eligibility.SID, Claims.Diag_Code1
***HAVING Count(IIf([Claims].[REV_CODE]='450',1,0))>1***
ORDER BY Eligibility.PHID & '-' & Eligibility.SID;

这让美元看起来更加合理。谢谢大家。

【讨论】:

以上是关于支付金额大于支付总额的主要内容,如果未能解决你的问题,请参考以下文章

一次查询支付多张发票

根据 Woocommerce 中的购物车总金额添加或删除支付网关

Paypal自适应支付(并行)550001错误

WooCommerce:根据选择的支付网关向卡添加费用

用例设计

sql支付分发