1

我们正在从数据系统转移到另一个系统。我不得不重写一些报告,新系统有一个自定义报告模块,您可以在其中编写查询并将其粘贴到窗口中并将其推送给最终用户。我有一份所有用户都在请求的报告,但我不知道如何编写它。

它按年级、性别和种族对学校人口进行了细分。见下文附件。

在此处输入图像描述

我写了这篇文章,按年级划分了人口,但不知道从哪里开始。

SELECT
gl.title as Grade,
COUNT (s.student_id)
FROM
students s,
student_enrollment se,
school_gradelevels gl
WHERE
s.student_id = se.student_id
AND se.school_id=gl.school_id
AND se.grade_id=gl."id"
AND se.syear =2012
AND se.end_date IS NULL
AND se.school_id =10

group by gl.title

order by Grade;

作为参考,性别是 s.gender,种族是 s.race。我想知道是否将不得不购买像水晶报告这样的报告软件。

4

3 回答 3

1

从根本上说,您必须按您计算的所有内容进行分组,这似乎是年级、种族和性别。首先做到这一点:将所有内容排成一行,按照您需要的方式进行总结。

可能让您感到困惑的是,该报告有两个级别的分组:一个按年级、种族和性别分组,一个按年级和种族分组。实际上,它看起来像这样:

create view V as 
SELECT   gl.title as Grade
       , s.sex
       , s.race 
       , COUNT (s.student_id) as Q
FROM students as s
JOIN student_enrollment as se
ON s.student_id = se.student_id
JOIN school_gradelevels gl
ON se.school_id = gl.school_id
AND se.grade_id = gl.id
WHERE
    se.syear = 2012
AND se.end_date IS NULL
AND se.school_id = 10
group by gl.title, s.sex, s.race

select Grade, race, sex, Q
from V
UNION
select Grade, race, 'Z', sum(Q) as Q
from V
group by Grade, race

如何将其转换为交叉表报告取决于您可以使用的工具。在 SQL 中可以使用运算pivot符或case. select诀窍是要注意每个种族都需要三列:男性、女性和两者。有点繁琐,这里有一个示例

select   Grade, 1 as sort_order  
       , max(case race when 1 then 
                  case sex when 'M' then Q end end) as r1mq
       , NULL as r1mb
       , max(case race when 1 then 
                  case sex when 'F' then Q end end) as r1fq
       , max(case race when 2 then 
                  case sex when 'M' then Q end end) as r2mq
       , NULL as r2mb
       , max(case race when 2 then 
                  case sex when 'F' then Q end end) as r2fq
       ...
from V group by Grade
UNION
select   Grade, 2 as sort_order  
       , NULL as r1mq
       , max(case race when 1 then 
                  case sex when 'Z' then Q end end) as r1bq
       , NULL as r1fq
       , NULL as r2mq
       , max(case race when 2 then 
                  case sex when 'Z' then Q end end) as r2bq
       , NULL as r2fq
       ...
from V group by Grade
order by Grade, sort_order

这会产生每个年级的两行,每个年级的第二行中的合并学生总数。

你明白了。您还需要查询来计算按年级和性别分组的学生,以及仅按年级分组的学生。这些可以加入到您的数据透视查询的输出中。并且按种族的总数,对于最后一行,与等级 99 或其他东西结合在一起,使其排在最后。结果将与报告的行和列布局相匹配。

于 2013-04-18T07:11:44.587 回答
1
with detail as (
    select
        gl.title as grade, s.race, s.gender, count(s.student_id) total
    from
        students s
        inner join
        student_enrollment se using (student_id)
        inner join
        school_gradelevels gl on se.school_id = gl.school_id and se.grade = gl.id
    where
        and se.syear = 2012
        and se.end_date is null
        and se.school_id = 10
    group by 1, 2, 3
    order by 1, 2, 3
) agg as (
    select grade, array_agg(total) total
    from detail
    group by grade
)
select
    grade,
    (select sum(e) from unnest(total) s(e)) total_grade,
    total[1,1] + total[2,1] + total[3,1] + total[4,1] + total[5,1] + total[6,1] total_F,
    total[1,2] + total[2,2] + total[3,2] + total[4,2] + total[5,2] + total[6,2] total_M,
    total[1,1] total_A_F,
    total[1,2] total_A_M,
    total[1,1] + total[1,2] total_A,
    total[2,1] total_B_F,
    total[2,2] total_B_M,
    total[2,1] + total[2,2] total_B,
    total[3,1] total_H_F,
    total[3,2] total_H_M,
    total[3,1] + total[3,2] total_H,
    total[4,1] total_I_F,
    total[4,2] total_I_M,
    total[4,1] + total[4,2] total_I,
    total[5,1] total_M_F,
    total[5,2] total_M_M,
    total[5,1] + total[5,2] total_MM,
    total[6,1] total_W_F,
    total[6,2] total_W_M,
    total[6,1] + total[6,2] total_W,
from agg
于 2013-04-18T13:34:31.973 回答
1

没有数据示例,因此可以假定针对学生捕获了性别和种族信息。

你想要做的是得到一个看起来像这样的初始表:

GradeLevel   |   Category     |  NumOfStudents
------------------------------------------------
PK           |     B - Male   |  10
PK           |     B - Female |  12
0            |     B - Male   |  5
0            |     B - Female |  6

这很容易通过执行以下操作来实现:

SELECT
gl.title as GradeLevel,
s.Race + '-' +s.Gender as Category
COUNT (s.student_id) as NumOfStudents
FROM 
           students s,
inner join student_enrollment se on s.student_id = se.student_id,
inner join school_gradelevels gl on se.school_id=gl.school_id AND se.grade_id=gl."id"
WHERE
    se.syear =2012
AND se.end_date IS NULL
AND se.school_id =10

group by gl.title, s.Race + '-' +s.Gender

order by Grade;

接下来,您将希望通过使用您的报告工具天气它的 SSRS 或 Crystal 或任何其他支持矩阵报告的东西来从该数据中进行数据透视。

如果没有可用的报告工具知道如何制作矩阵,请使用 SQLPivot子句: http: //msdn.microsoft.com/en-us/library/ms177410 (v=sql.105).aspx

SELECT GradeLevel, [B-Male], [B-Female] ....
FROM 
  (
      SELECT
      gl.title as GradeLevel,
      s.Race + '-' +s.Gender as Category
      COUNT (s.student_id) as NumOfStudents
      FROM 
                 students s,
      inner join student_enrollment se on s.student_id = se.student_id,
      inner join school_gradelevels gl on se.school_id=gl.school_id AND se.grade_id=gl."id"
      WHERE
          se.syear =2012
      AND se.end_date IS NULL
      AND se.school_id =10
      group by gl.title, s.Race + '-' +s.Gender   
  ) rawData
  PIVOT
  (
  SUM(NumOfStudents)
  FOR Category IN ( [B-Male], [B-Female], ... )
) AS pvt 
于 2013-04-18T03:03:55.303 回答