这是我当前的WIP查询:
select
iclaim_id,
(select SUM(AuthAmtLineItems) as AuthAmt
from
(select
cauth_total as AuthAmtLineItems
from
claim_details_history
where
iclaim_id = cd.iclaim_id
and iclaim_det_status_id = 2
and btcovered_flag = 1
group by
irecord_id, sdetail_type, sdetail_desc, cauth_total) AuthAmt
having
SUM(AuthAmtLineItems) > 750) as AuthAmt
from
claim_details_history cd
where
iclaim_det_status_id = 2
and btcovered_flag = 1
group by
iclaim_id
having
min(dtupdate_last) between '02/05/2015 00:00:00.000' and '02/05/2015 23:59:59.997'
以下是结果集:
iclaim_id AuthAmt
67712 3500.00
71054 NULL
71032 NULL
71096 NULL
68824 NULL
71039 NULL
71071 NULL
67863 NULL
71098 NULL
70437 1500.00
71048 NULL
71037 NULL
71080 NULL
71035 NULL
71049 NULL
71118 NULL
71053 759.14
我正在尝试和未能做的是删除其中包含NULL的行或having SUM(AuthAmtLineItems) > 750
哪个选项更容易。
答案 0 :(得分:1)
而不是correlated subquery
使用cross apply
SELECT iclaim_id,
AuthAmt
FROM claim_details_history cd
CROSS apply (SELECT Sum(AuthAmtLineItems) AS AuthAmt
FROM (SELECT cauth_total AS AuthAmtLineItems
FROM claim_details_history
WHERE iclaim_id = cd.iclaim_id
AND iclaim_det_status_id = 2
AND btcovered_flag = 1
GROUP BY irecord_id,
sdetail_type,
sdetail_desc,
cauth_total) AuthAmt
HAVING Sum(AuthAmtLineItems) > 750) cs
WHERE iclaim_det_status_id = 2
AND btcovered_flag = 1
GROUP BY iclaim_id
HAVING Min(dtupdate_last) BETWEEN '02/05/2015 00:00:00.000' AND '02/05/2015 23:59:59.997'