为什么我的查询没有返回正确的结果?

时间:2016-01-27 09:25:39

标签: sql sql-server sql-server-2008 tsql

我已经使用这些Temp表来从DIstrict分组的同一表中返回SOlved案例和待处理案例总数的总数,例如

mLatch.await();
// Launch new activity here

我已经这样做但是这不会返回正确的结果

District TotalSolvedCases TotalPendingCases
A                3             1
B                8             6
C                7             1

1 个答案:

答案 0 :(得分:4)

您只需要一个SELECT,使用case表达式进行条件计数:

SELECT COUNT(case when CaseHearings.IsClosingDate = 1 then 1 end) TotalCases,
       COUNT(case when CaseHearings.IsClosingDate = 0 then 1 end) TotalPedningCases,
       Districts.DistrictName
FROM Cases
INNER JOIN ConcernedOffices ON ConcernedOffices.pk_ConcernedOffices_ID = Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID
INNER JOIN Districts ON Districts.pk_Districts_DistrictID = ConcernedOffices.fk_Districts_ConcernedOffices_DistrictID
INNER JOIN CaseHearings ON CaseHearings.fk_Cases_CaseHearings_CaseID = Cases.pk_Cases_CaseID
GROUP BY Districts.DistrictName