1

嗨,我无法在此查询中找到行总和 -

表格如下:

Master_Choicecode

ChoiceCode      MainCourseId    CourseLevelId    InstituteId

Master_MainCourse

MainCourseId     MainCourseName   CourseLevelId   CourseProgram
  11                x                1             abc
  12                y                2             xyz

Master_CourseLevel

CourseLevelId    CourseLevelName     
   1                deg
   2                Dip

硕士_研究所

Instituteid     InstituteName    Statusid
  1001           Insti1          100
  1002           Insti2          200

Master_InstituteStatus

StatusId   StatusName
 100            Status1
 200            Status2

现在使用所有这些表我想展示这个:

CourseProgram   CourseLevelName  Status1(from Master_InstituteStatus)  Status2(from Master_InstituteStatus) TotalInstitutes
abc             Deg          Count(status1institute)  Count(status2institute)             Total(status1+status2)

现在这是我尝试过的:

SELECT B.CourseProgram,C.CourseLevelName,
sum(case when E.InstituteStatusName =' Status1' then 1 else 0 end )as     Status1,
sum(case when E.InstituteStatusName =' Status2' then 1 else 0  end ) as  Status2,
FROM Master_ChoiceCode A 
inner join Master_MainCourse B on A.MainCourseID=B.MainCourseID
inner join Master_CourseLevel C on A.CourseLevelID=C.CourseLevelID  
inner join  Master_Institute D on A.InstituteID=D.InstituteID
inner join Master_InstituteStatus1 E on D.InstituteStatusID1=E.InstituteStatusID
where B.CourseLevelID IN(1,2)
GROUP BY B.CourseProgram,A.CourseLevelID,C.CourseLevelName,E.InstituteStatusName
order by B.CourseProgram,C.CourseLevelName;

有了这个,我得到这样的输出:

CourseProgram   CourseLevelName  Status1(from Master_InstituteStatus)     Status2(from Master_InstituteStatus)    
  abc              deg                10(count of status1)                 20(count of status2)

请告诉我应该如何找到 Status1 列和 Status2 列的总和,以便我可以将最后一列作为 Total_No_ of_institutes

4

2 回答 2

1
SELECT B.CourseProgram,C.CourseLevelName,
sum(case when E.InstituteStatusName =' Status1' then 1 else 0 end )as     Status1,
sum(case when E.InstituteStatusName =' Status2' then 1 else 0  end ) as  Status2,
sum(case when E.InstituteStatusName IN (' Status1', ' Status2') then 1 else 0  end ) as  Total_No_of_institutes,

FROM Master_ChoiceCode A 
inner join Master_MainCourse B on A.MainCourseID=B.MainCourseID
inner join Master_CourseLevel C on A.CourseLevelID=C.CourseLevelID  
inner join  Master_Institute D on A.InstituteID=D.InstituteID
inner join Master_InstituteStatus1 E on D.InstituteStatusID1=E.InstituteStatusID
where B.CourseLevelID IN(1,2)
GROUP BY B.CourseProgram,A.CourseLevelID,C.CourseLevelName,E.InstituteStatusName
order by B.CourseProgram,C.CourseLevelName;
于 2012-09-05T05:39:03.353 回答
0
SELECT B.CourseProgram,C.CourseLevelName,
sum(case when E.StatusName=' Status1' then 1 else 0 end )as Status1,
sum(case when E.StatusName=' Status2' then 1 else 0  end ) as  Status2,
FROM Master_ChoiceCode A 
inner join Master_MainCourse B on A.MainCourseID=B.MainCourseID
inner join Master_CourseLevel C on A.CourseLevelID=C.CourseLevelID  
inner join  Master_Institute D on A.InstituteID=D.InstituteID
inner join Master_InstituteStatus1 E on D.InstituteStatusID1=E.InstituteStatusID
where B.CourseLevelID IN(1,2)
GROUP BY B.CourseProgram,A.CourseLevelID,C.CourseLevelName,E.InstituteStatusName
order by B.CourseProgram,C.CourseLevelName;
于 2012-09-05T05:36:39.993 回答