SQL查找先前的记录信息

时间:2020-01-28 20:39:04

标签: sql sql-server performance runtime

这是我的情况。我对一个人有一系列要求。我们有时会得到重复的索赔,该索赔给出了DUP错误代码,并且被零美元金额拒绝。我要尝试的是查找原始索赔单位和开票金额。如果重复的索赔和原始的索赔是相同的单位和计费金额,则我打算忽略它(或至少将其标记为不是潜在的重新开票)。如果单位和/或计费金额不同,则该索赔将被标记为潜在的重新开票。

我有一个可以正确找到原始声明主键值的函数,并且基本查询在不到一秒钟的时间内运行。但是,当我尝试将该数据集链接回表时,将我的运行时间浪费到了无用的地步。我不明白的是,为什么单独运行该功能的速度如此之快,却又试图将其链接回原来的速度如此之高,我们正在谈论的是在一年的活动中有140个宣称的数据集。

如果有人可以提供一些见识或有更好的方法来实现这一目标,我将有义务。

SELECT pre.*
--, st.unitsofservice as OrigUnits
--I only include the line above if the link to the servicetransaction table on the last line of the query is active

FROM
    (
    SELECT Sub.*--, 
        ,dbo.cf_DuplicateServiceSTID(sub.servicetransactionid) as PaidSvc
--the line above is the function returning the primary key value for the original claim
    FROM 
        (
        SELECT Pre.servicetransactionID, 
            st.servicedate, st.individualid, st.agencyid, st.servicecode, st.placeofserviceid, st.unitsofservice,
            dbo.sortmodifiers(st.modifiercodeid, st.modifiercodeid2, st.modifiercodeid3, st.modifiercodeid4) as Modifiers, 
            bd.billedamount,
            a.upi,
            b.name
        FROM (select pmt.servicetransactionid
                from pmtadjdetail pmt
                where substring(pmt.reasoncodes,1,5) = 'DUP') Pre
            JOIN servicetransaction st on pre.servicetransactionid = st.servicetransactionid
            join billdetail bd on st.servicetransactionid = bd.servicetransactionid
            join agency a on st.agencyid = a.agencyid
            join business b on a.businessid = b.businessid
        where st.servicedate between @StartDate and @EndDate
            and st.agencyid = iif(@AgencyID is null, st.agencyid, @AgencyID)
        ) Sub
        join individual i on sub.individualid = i.individualid
        join enrollment e on sub.individualid = e.individualid
    WHERE e.enrollmenttype <> 'p'
    ) Pre
--join servicetransaction st on pre.paidsvc = st.servicetransactionid
--If I remove the comment from the line above my run time increases to the point of uselessness

0 个答案:

没有答案