我在case语句中使用sum()。但这会减慢我的查询速度。还有其他办法吗?这是查询。请帮帮我。
SELECT (SUM(PRI_INS_AGING) + SUM(SEC_INS_AGING) + SUM(TER_INS_AGING)) AS INS_AGING,SUM(PAT_AGING) AS PAT_AGING FROM
(SELECT
CASE WHEN L.RESP_PARTY =1 AND VP.STATUS IN(3,5) AND VP.PRIMARY_PAID =0 AND VP.PRIMARY_PENDING >0 AND C.PRIMARY_PAYER_ID >0 AND C.HIDEN=0 THEN SUM(L.AMOUNT) ELSE 0 END AS PRI_INS_AGING,
CASE WHEN L.RESP_PARTY =2 AND VP.STATUS IN(6,7,5) AND VP.SECONDARY_PAID =0 AND VP.SECONDARY_PENDING >0 AND VP.PRIMARY_PENDING <=0 AND C.SECONDARY_PAYER_ID >0 AND C.HIDEN=0 THEN SUM(L.AMOUNT) ELSE 0 END AS SEC_INS_AGING,
CASE WHEN L.RESP_PARTY =3 AND VP.STATUS IN(8,9,5) AND VP.TERTIARY_PAID =0 AND VP.TERTIARY_PENDING >0 AND VP.PRIMARY_PENDING <=0 AND VP.SECONDARY_PENDING <=0 AND C.TERTIARY_PAYER_ID >0 AND C.HIDEN=0 THEN SUM(L.AMOUNT) ELSE 0 END AS TER_INS_AGING,
CASE WHEN L.RESP_PARTY =4 THEN SUM(L.AMOUNT) ELSE 0 END AS PAT_AGING
FROM VISIT_PROCEDURE VP
JOIN CLAIM C
ON (C.CLAIM_ID = VP.CLAIM_ID AND C.CLINIC_ID = VP.CLINIC_ID)
JOIN LEDGER L
ON (L.CLAIM_ID = L.CLAIM_ID AND VP.CLINIC_ID = L.CLINIC_ID)
WHERE C.CLINIC_ID = 34847 AND L.TYPE IN(1,8,9,10,11) AND L.ACTIVE=1
GROUP BY VP.PROCEDURE_ID,L.TYPE,L.RESP_PARTY,L.ACTIVE)T1
由于 苏尼
答案 0 :(得分:2)
在没有额外解析你的SQL的情况下,查看你在LEDGER上的加入......你让它在相同的值上做了CLAIM_ID ......
JOIN LEDGER L ON L.CLAIM_ID = L.CLAIM_ID AND VP.CLINIC_ID = L.CLINIC_ID
“L.Claim_ID”应该加入“VP.Claim_ID”吗?或其他什么?
好的,所以有一点时间,我想出了这个...我会把查询换成一些。此外,请确保在Clinic_ID AND Hiden上的CLAIM表上有索引。此外,您的内部查询正在分解保险索赔的不同部分的SUM,但除了在外部对它们进行求和之外,您没有对它们执行任何操作。我会改为在给定条件下在外部加上ONCE
SELECT STRAIGHT_JOIN
SUM( IF( L.RESP_PARTY = 1
AND VP.STATUS IN(3,5)
AND VP.PRIMARY_PAID = 0
AND VP.PRIMARY_PENDING > 0
AND C.PRIMARY_PAYER_ID > 0, L.AMOUNT, 0 )
+
IF( L.RESP_PARTY = 2
AND VP.STATUS IN(6,7,5)
AND VP.SECONDARY_PAID = 0
AND VP.SECONDARY_PENDING > 0
AND VP.PRIMARY_PENDING <= 0
AND C.SECONDARY_PAYER_ID > 0, L.AMOUNT, 0 )
+
IF( L.RESP_PARTY = 3
AND VP.STATUS IN(8,9,5)
AND VP.TERTIARY_PAID = 0
AND VP.TERTIARY_PENDING > 0
AND VP.PRIMARY_PENDING <= 0
AND VP.SECONDARY_PENDING <= 0
AND C.TERTIARY_PAYER_ID > 0, L.AMOUNT, 0 ) ) as INS_AGING,
SUM( IF( L.RESP_PARTY = 4, L.AMOUNT, 0 )) as PAT_AGING
FROM
CLAIM C
JOIN VISIT_PROCEDURE VP
ON C.CLAIM_ID = VP.CLAIM_ID
AND C.CLINIC_ID = VP.CLINIC_ID
JOIN LEDGER L
ON VP.CLINIC_ID = L.CLINIC_ID
AND VP.CLAIM_ID = L.CLAIM_ID
AND L.TYPE IN ( 1, 8, 9, 10, 11 )
AND L.ACTIVE = 1
WHERE
C.CLINIC_ID = 34847
AND C.HIDEN = 0
答案 1 :(得分:0)
如果您希望获得数据的SUM(),我不会看到您以任何其他方式获取它。也就是说,这非常复杂,您可以将其分解为存储过程并将计算分成几个阶段。
顺便说一下,这里的缓慢意味着什么? 1秒而不是0.001?或者,2分钟?