SQL Aggregate具有分组功能

时间:2015-02-19 12:06:40

标签: sql aggregate partition

我需要创建一些检查,以确保学生注册正确的课程数量正确。这是我目前的SQL。

  SELECT StudentID
  ,AssessmentCode
  ,BoardCode
  ,BoardCategory
  ,BoardUnits
  ,sum(cast(boardunits as int)) over (partition by studentid,boardcategory) as UnitCount
  ,Count(boardcategory) over (partition by studentid)  as SubjectCount
FROM uvNCStudentSubjectDetails
where fileyear = 2015 
and filesemester = 1
and studentyearlevel = 11 
and StudentIBFlag = 0
order by Studentnameinternal,BoardCategory

这给了我以下信息......

StudentID   AssessmentCode  BoardCode   BoardCategory   BoardUnits  UnitCount   SubjectCount
61687       11TECDAT        11080             A             2          11           7
61687       11PRS1U         11350             A             1          11           7
61687       11MATGEN        11235             A             2          11           7
61687       11LANGRB        11870             A             2          11           7           
61687       11ENGSTD        11130             A             2          11           7
61687       11GEOGEO        11190             A             2          11           7
64549       11TECIND        11200             A             2          10           7
64549       11SCIPHY        11310             A             2          10           7
64549       11SCIEAE        11100             A             2          10           7
64549       11MATGEN        11235             A             2          10           7
64549       11ENGSTD        11130             A             2          10           7
64549       11TECHOS        26501             B             2          2            7
64549       11MUSDRS        63212             C             1          1            7
45461       11ECOECO        11110             A             2          13           7
45461       11ENGADV        11140             A             2          13           7
45461       11HISMOD        11270             A             2          13           7
45461       11HISLST        11220             A             2          13           7
45461       11MATMAT        11240             A             2          13           7
45461       11PRS1U         11350             A             1          13           7
45461       11SCIBIO        11030             A             2          13           7

对于第一个学生,请注意我有A类科目单元(总共11个)他只做A类科目。对于第二个学生,他有10个单元A类科目,他正在做1个B类科目,价值2个单元,1个C类科目,价值1个单元。最后的学生只有13个A类单位。

现在我真正想要的是这样......!

StudentID   Sum A Units Sum B Units Sum C Units Sum A Units + Sum B Units   Count of Subjects
61687          11           0           0                  11                 7
64549          10           2           1                  12                 7
45461          13           0           0                  13                 7

所以我想要一些聚合函数,学生只能分成1行,不同单位的总和作为单独的字段。我还想要一个总结A类和B类单位的字段,还有一个字段,它给出了他们正在做的主题总数的计数。然后,如果学生没有正确数量的A或B单位等,我可以使用这些数据设置一些警告信息

我玩过常见的表表达式,子查询等,但我不确定我在做什么,并且不确定以我想要的形式获取数据的正确方法。

有人能帮忙吗?

1 个答案:

答案 0 :(得分:0)

SELECT
STUDENTID,
SUM(CASE BOARDCATEGORY WHEN 'A' THEN 1 ELSE 0 END) AS SUM_A_UNITS,
SUM(CASE BOARDCATEGORY WHEN 'B' THEN 1 ELSE 0 END) AS SUM_B_UNITS,
SUM(CASE BOARDCATEGORY WHEN 'C' THEN 1 ELSE 0 END) AS SUM_C_UNITS,
SUM(CASE BOARDCATEGORY WHEN 'A' THEN 1 WHEN 'B' THEN 1 ELSE 0 END) AS SUM_A_UNITS+SUM_B_UNITS,
COUNT(BOARDCODE) AS COUNT_OF_SUBJECTS
FROM (
    SELECT StudentID
    ,AssessmentCode
    ,BoardCode
    ,BoardCategory
    ,BoardUnits
    ,sum(cast(boardunits as int)) over (partition by studentid,boardcategory) as UnitCount
    ,Count(boardcategory) over (partition by studentid)  as SubjectCount
    FROM uvNCStudentSubjectDetails
    where fileyear = 2015 
    and filesemester = 1
    and studentyearlevel = 11 
    and StudentIBFlag = 0
    order by Studentnameinternal,BoardCategory
)
GROUP BY STUDENTID;

在解决方案中包装您的SQL语句,以便您可以立即查看解决方案的作用。

使用SUM和CASE(即仅在满足条件时使用SUM)。