1

我如何在这里按问题解决分组问题。

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 子句中。

4

2 回答 2

6

ISNULL不是聚合函数,因此,当您进行分组时,它不会按预期执行。我会推荐SUM, MIN, MAX, 或AVG包裹您的ISNULL语句,具体取决于您要通过查询实现的目标。

于 2013-09-25T10:05:11.900 回答
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
于 2013-09-25T10:20:07.943 回答