按iSNULL功能分组

时间:2013-09-25 10:00:11

标签: sql-server tsql

如何解决此问题。

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

2 个答案:

答案 0 :(得分:6)

ISNULL不是聚合函数,因此,当您进行分组时,它将无法按预期执行。我建议SUMMINMAXAVG包围您的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