我需要一些帮助来计算以下两个数据集的滚动 3 个月平均成本。这是 Dataset1 / Dataset 2 的 3 个月平均值。我不确定使用 cte 是否是正确的路线。任何见解都值得赞赏。
Dataset 1:
SELECT(factAdmissions.ContractCode + '-' +factAdmissions.BenefitPlanCode) AS [Contract Code],
factAdmissions.AdmitCCYYMM,
ISNULL(sum(AmountPaid),0)As [Amount Paid]
FROM factAdmissions
WHERE factAdmissions.AdmitCCYYMM >='200701'
AND factAdmissions.AcuteSNFIndAdmit ='a'
AND factAdmissions.ContractCode Is Not Null
AND factAdmissions.BenefitPlanCode Is Not Null
AND BusinessUnitCode In('EP','HF', 'VN')
AND AdmitCCYYMM BETWEEN (@StartDate) AND (@EndDate)
AND factAdmissions.ContractCode + '-' +factAdmissions.BenefitPlanCode IN (@Product)
Group by factAdmissions.ContractCode,
factAdmissions.BenefitPlanCode,
factAdmissions.AdmitCCYYMM
Order by factAdmissions.ContractCode,
factAdmissions.BenefitPlanCode,
factAdmissions.AdmitCCYYMM;
Dataset2:
Select
(factMembership.ContractCode+'-'+ factMembership.BenefitPlanCode) As Product,
EffectiveCCYYMM,
ISNULL(count(Distinct MemberId),0) As MemberCount
From factMembership
Where EffectiveCCYYMM >= '200701'
AND BusinessUnitCode In('EP','HF', 'VN')
AND EffectiveCCYYMM BETWEEN (@StartDate) AND (@EndDate)
AND factMembership.ContractCode + '-' +factMembership.BenefitPlanCode IN (@Product)
Group by
ContractCode,
BenefitPlanCode,
EffectiveCCYYMM
Order by 1;
Dataset 1: Output 201101 201102 201103
Cost $420,627 $73,608 $93,792
Dataset 2:Output 201103
Memberscount 400
Desired Output: 201103
3 Month Rolling Average $490.02