如何解决此问题。
SELECT
Tcg.ProviderID,
ISNULL(DATEDIFF(DAY , AEenc.StartDateTime , AEenc.EndDateTime),0) + ISNULL(DATEDIFF(DAY , IPenc.AdmissionDate , IPenc.ENDDate),0) as 'IPBedDays'
FROM #TargetClientGroup Tcg
INNER JOIN AEncounterDetail as AEenc
ON AEenc.ProviderID = Tcg.ProviderID
INNER JOIN [PEncounterDetail] as IPenc
ON IPenc.ProviderID = Tcg.ProviderID
--WHERE AEenc.StartDateTime >= @ReportDate
GROUP BY Tcg.ProviderID
我收到错误,因为它未包含在聚合函数或GROUP BY子句中。
答案 0 :(得分:6)
ISNULL
不是聚合函数,因此,当您进行分组时,它将无法按预期执行。我建议SUM
,MIN
,MAX
或AVG
包围您的ISNULL
语句,具体取决于您尝试通过查询实现的内容。< / p>
答案 1 :(得分:0)
试试这个 -
SELECT Tcg.ProviderID
, IPBedDays = SUM(ISNULL(DATEDIFF(DAY, AEenc.StartDateTime, AEenc.EndDateTime), 0) + ISNULL(DATEDIFF(DAY, IPenc.AdmissionDate, IPenc.EndDate), 0))
FROM #TargetClientGroup Tcg
JOIN AEncounterDetail AEenc ON AEenc.ProviderID = Tcg.ProviderID
JOIN [PEncounterDetail] IPenc ON IPenc.ProviderID = Tcg.ProviderID
--WHERE AEenc.StartDateTime >= @ReportDate
GROUP BY Tcg.ProviderID