0

我是 postgresql 交叉表函数的新手,并且在 SO 上尝试了一些解决方案,但仍然卡住了。所以基本上我有一个查询,导致如下输出:

|student_name|subject_name|marks|
|------------|------------|-----|
|John Doe    |ENGLISH     |65   |
|John Doe    |MATHEMATICS |72   |
|Mary Jane   |ENGLISH     |74   |
|Mary Jane   |MATHEMATICS |70   |
|------------|------------|-----|

我使用交叉表的目标是:

|student_name| ENGLISH | MATHEMATICS |
|------------|---------|-------------|
|John Doe    | 65      | 72          |
|Mary Jane   | 74      | 70          |
|------------|---------|-------------|

我返回第一个表(没有交叉表)的查询是:

SELECT student_name, subject_name, sum(marks) as marks FROM (
    SELECT student_id, student_name, class_name, exam_type, subject_name, total_mark as marks, total_grade_weight as out_of, percentage, grade, sort_order
    FROM(
        SELECT  student_id, student_name, class_name, exam_type, subject_name, total_mark, total_grade_weight, ceil(total_mark::float/total_grade_weight::float*100) as percentage,
            (select grade from app.grading where (total_mark::float/total_grade_weight::float)*100 between min_mark and max_mark) as grade, sort_order
        FROM (
            SELECT --big query with lots of JOINS
        ) q ORDER BY sort_order
    )v GROUP BY v.student_id, v.student_name, v.class_name, v.exam_type, v.subject_name, v.total_mark, v.total_grade_weight, v.percentage, v.grade, v.sort_order
    ORDER BY student_name ASC, sort_order ASC
    )a
    GROUP BY student_name, subject_name
ORDER BY student_name

对于交叉表,这是我被列卡住的地方。

SELECT * FROM 
crosstab(' //the query above here ',
          $$VALUES ('MATHEMATICS'::text), ('marks')$$
        ) AS ct 
(student_name text, subject_name character varying, marks numeric);

如果我如上所示运行它,这就是我最终得到的结果:

|student_name|subject_name|marks|
|------------|------------|-----|
|John Doe    | 65         |     |
|Mary Jane   | 74         |     |
|____________|____________|_____|

正如它所说的那样,subject_name不是ENGLISHMATHEMATICS。显然,现在我看到我不需要该marks列,但我怎样才能让它将所有主题名称作为列名?他们可能是两个,他们可能是十二个。

4

1 回答 1

0

解决了它,但我更喜欢动态的解决方案。我换了这个;

$$VALUES ('MATHEMATICS'::text), ('marks')$$有了这个;

'SELECT subject_name FROM app.subjects WHERE ... ORDER BY ...'我的解决方案的缺点是最后一部分更改为

(student_name text, english bigint, mathematics bigint, physics bigint, biology bigint, chemistry bigint, history bigint, ...);

也就是说,我必须手动列出所有主题,并且完全按照它们从上面的选择中列出的顺序。我觉得这不是很方便,但它有效。

于 2018-03-14T07:41:00.100 回答