2013-09-25 73 views
1

如何通過問題解決組。通過iSNULL功能發佈組

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

回答

5

ISNULL不是一個聚合函數,因此,當你分組時它不會按預期執行。我會建議SUM,MIN,MAXAVG圍繞您的ISNULL聲明執行,這取決於您試圖通過查詢實現的目標。

+0

這看起來像一個很好的解決辦法。它對解決方案的工作很好。感謝您的幫助。 – user2637506

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