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

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)

最後の列をTotal_No_ of_institutesとして取得できるように、Status1列とStatus2列の合計を見つける方法を教えてください

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 に答える