我想找到供应商组的总成本,以12个月的平均值计算。
SELECT
sum(AEBill.[TotalCost])
,AEEnc.ProviderID
,AEEnc.Date
INTO #TotalCostAccuteCareAE
FROM EncDetail AEEnc
INNER JOIN [EncBilling] as AEBill
ON AEBill.[SK_EncID] = AEEnc.[SK_EncID]
AND AEBill.ProviderID = AEEnc.ProviderID
WHERE AEEnc.Date <= reportDate
GROUP BY AEBill.ProviderID
此查询仅选择小于或等于报告日期的所有记录的总和。但我想要从给定月份开始的12个月的平均成本。
答案 0 :(得分:0)
看一下这个问题SQL Server: Get data for only the past year
SELECT
AVG(AEBill.[TotalCost])
,AEEnc.ProviderID
,AEEnc.Date
INTO #TotalCostAccuteCareAE
FROM EncDetail AEEnc
INNER JOIN [EncBilling] as AEBill
ON AEBill.[SK_EncID] = AEEnc.[SK_EncID]
AND AEBill.ProviderID = AEEnc.ProviderID
WHERE AEEnc.Date <= DATEADD(year,-1,GETDATE())
GROUP BY AEBill.ProviderID