无法执行查询分组

时间:2012-08-30 07:00:14

标签: sql sql-server sql-server-2008 pivot

我想水平旋转我的桌子 -

表格如下:

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)    
    abc             Deg          Count of institutes belonging to status1  Count of institutes belonging to status2

现在这就是我的尝试:

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;

但是我得到了这样的输出:

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

我得到的解决方案是使用数据库...但我不知道如何使用我的当前查询Pivot。请帮助我..

2 个答案:

答案 0 :(得分:1)

您已关闭,但在E.InstituteStatusName上进行分组意味着您无法将数据合并到一行。而不是将case语句与count一起使用,您可以使用条件和。

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
order by B.CourseProgram,C.CourseLevelName;

这将有效地计算与该状态匹配的行,而无需在该字段上进行分组。

答案 1 :(得分:1)

您可以在SQL-Server中使用内置的PIVOT函数:

WITH CTE AS
(   SELECT  B.CourseProgram,
            C.CourseLevelName, 
            E.InstituteStatusName,
            b.CourseLevelID
    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)
)
SELECT  CourseProgram, CourseLevelName, [Status1], [Status2]
FROM    CTE
        PIVOT
        (   COUNT(CourseLevelID)
            FOR InstituteStatusName IN ([Status1], [Status2])
        ) pvt

ORDER BY CourseProgram, CourseLevelName;