このクエリを実行するより効率的な方法はありますか?それは実行されますが、完了するまでに約6分かかります。私は複数の選択ステートメントが遅延を作成していると思います。いずれかを実行すると、結果は<に戻ります。 PROV_COMPANY
とPROV_ADDINFO
間のMの関係:MEMB_COMPANY
とPROV_COMPANY
と1の間のMの関係:複数の列に選択を使用
SELECT
x.HPCODE AS [Health Plan],
x.HPFROMDT AS [Eff Date],
x.MEMBID AS [ID #],
x.LASTNM AS [Mbr L Name],
x.FIRSTNM AS [Mbr F Name],
x.BIRTH AS DOB,
DATEDIFF(YEAR, birth, GETDATE()) AS Age,
x.PCP as [Prov ID#],
Y.LASTNAME as [Prov L Name],
Y.FIRSTNAME as [Prov F Name],
PA.PHONE as [Provider Phone],
(SELECT MAX(cd.FROMDATESVC)
FROM [dbo].[Claim_Masters] cm
LEFT JOIN [dbo].[Claim_Details] cd ON cm.claimno = cd.claimno
AND cm.MEMBID = x.MEMBID
WHERE LEFT(cd.PROCCODE, 5) IN ('G0402', 'G0438', 'G0439')
AND YEAR(cm.SERVICEDATEFROM) = YEAR(GETDATE())) AS [AWV],
(SELECT MAX(cd.FROMDATESVC)
FROM [dbo].[Claim_Masters] cm
LEFT JOIN [dbo].[Claim_Details] cd ON cm.claimno = cd.claimno
AND cm.MEMBID = x.MEMBID
WHERE (LEFT(cd.PROCCODE, 5) IN ('G0402', 'G0438', 'G0439')
OR RIGHT(cd.PROCCODE, 5) IN ('99490', '99487', '99489'))
AND YEAR(cm.SERVICEDATEFROM) = YEAR(GETDATE())) AS [CCM],
(select max(cd.FROMDATESVC) from [dbo].[Claim_Masters] cm left join [dbo].[Claim_Details] cd on cm.claimno = cd.claimno and cm.MEMBID = x.MEMBID Where (Right(cd.PROCCODE,5) in ('99495','99496'))and Year(cm.SERVICEDATEFROM) = Year(Getdate())) as [TCM],
(select max(cd.FROMDATESVC) from [dbo].[Claim_Masters] cm left join [dbo].[Claim_Details] cd on cm.claimno = cd.claimno and cm.MEMBID = x.MEMBID Where (Right(cd.PROCCODE,5) in ('99211','99212','99213','99214','99215','99201','99202','99203','99204','99205'))and Year(cm.SERVICEDATEFROM) = Year(Getdate())) as [Office],
(select max(cd.FROMDATESVC) from [dbo].[Claim_Masters] cm left join [dbo].[Claim_Details] cd on cm.claimno = cd.claimno and cm.MEMBID = x.MEMBID Where (Right(cd.PROCCODE,5) = '1170F')and Year(cm.SERVICEDATEFROM) = Year(Getdate())) as [FUNC],
(select max(cd.FROMDATESVC) from [dbo].[Claim_Masters] cm left join [dbo].[Claim_Details] cd on cm.claimno = cd.claimno and cm.MEMBID = x.MEMBID Where (Right(cd.PROCCODE,5) in ('1157F','1158F'))and Year(cm.SERVICEDATEFROM) = Year(Getdate())) as [ACP],
(select max(cd.FROMDATESVC) from [dbo].[Claim_Masters] cm left join [dbo].[Claim_Details] cd on cm.claimno = cd.claimno and cm.MEMBID = x.MEMBID Where (Right(cd.PROCCODE,5) in ('0521F','1125F','1126F'))and Year(cm.SERVICEDATEFROM) = Year(Getdate())) as [PAIN],
(select max(cd.FROMDATESVC) from [dbo].[Claim_Masters] cm left join [dbo].[Claim_Details] cd on cm.claimno = cd.claimno and cm.MEMBID = x.MEMBID Where (Right(cd.PROCCODE,5) in ('99605','99606','1160F','1111F','1159F'))and Year(cm.SERVICEDATEFROM) = Year(Getdate())) as [MTM]
FROM
[dbo].[MEMB_COMPANY] X
LEFT JOIN
[dbo].[PROV_COMPANY] Y ON X.PCP = Y.PROVID
JOIN
[dbo].[PROV_ADDINFO] PA ON Y.PROV_MPI_NO = PA.PROV_MPI_NO
AND (PA.EDI_DEFAULT = 1)
WHERE
(ISNULL(x.OPTHRUDT, '') = '' OR
x.OPTHRUDT > GETDATE())
AND X.HPCODE = 'CHPS'
使用 'JOIN' ???? – Ravi
あなたの目標は何か分かっていますか?メンバーは複数のクレームを持つことができますか?可能であれば、サブクエリは異なるクレームから日付を引き出すことができます(メンバーIDによってそれらを関連付けるだけなので)。したがって、あるクレームはTCMを供給し、別のクレームはACPを供給するかもしれない。 – SMor
このクエリはサポートできません。あなたの同僚やあなた自身について1か2か月後に考える。 – svgrafov