如何在Postgresql交叉表中获取动态列数



我是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_nameENGLISHMATHEMATICS.显然,现在我看到我不需要marks列,但是如何让它将所有主题名称作为列名称?他们可能是两个,也可能是12个。

解决了

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

$$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, ...);

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

相关内容

  • 没有找到相关文章

最新更新