i want to rotate my table horizontally –
The tables are as follows:
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
Now using all these tables i want to show this:
CourseProgram CourseLevelName Status1(from Master_InstituteStatus) Status2(from Master_InstituteStatus)
abc Deg Count of institutes belonging to status1 Count of institutes belonging to status2
Now this is what i have tried:
SELECT B.CourseProgram,C.CourseLevelName,
case when E.InstituteStatusName =' Status1' then COUNT(*) else null end as Status1,
case when E.InstituteStatusName =' Status2' then COUNT(*) else null 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;
But with this i get the output like this:
CourseProgram CourseLevelName Status1(from Master_InstituteStatus) Status2(from Master_InstituteStatus)
abc Deg Count of institutes belonging to status1
abc Deg Null Count of institutes belonging to status1
i got the solution to this is to use pivot…but i dont knw how shal i use Pivot with my current query.Please help me..
You’re close, but grouping on
E.InstituteStatusNamemeans you will not be able to combine the data onto one row. Rather than use thecasestatement with acount, you could use a conditional sum.This will effectively count the rows matching that status without the need for grouping on that field.