根据sql server中的某些条件查找行总和

时间:2012-09-05 05:32:49

标签: sql sql-server sql-server-2008-r2

您好我无法在此查询中找到行总和 -

表格如下:

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

Master_Institute

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;