首页 > 解决方案 > 计算所有表记录计数的麻烦

问题描述

我已经尝试使用子查询编写 sql 我已经实现了我的表的 count() 但是我们知道子查询需要很多基数数据库资源我希望我的 count( ) 计数作为列显示让假设我的表总共有 6 条记录当我计算我的表的计数(*)时,它将显示为:

6
6
6
6
6
6

我想在没有子查询的情况下实现上述记录。

WITH survey AS (
    SELECT
        *
    FROM
        (
            SELECT
                student_id,
                performance,
                teacher_behaviour,
                syllabus,
                shift_incharge_behaviour,
                punctuality,
                teaching_status,
                voice_clarity_class,
                daily_class,
                exams_status,
                mtm_helpline_status,
                fee_dept_status,
                personality_status,
                madni_task_assessment,
                survey_id
            FROM
                survey_feedback
            WHERE
                survey_id = 1
            GROUP BY
                student_id,
                performance,
                teacher_behaviour,
                syllabus,
                shift_incharge_behaviour,
                punctuality,
                teaching_status,
                voice_clarity_class,
                daily_class,
                exams_status,
                mtm_helpline_status,
                fee_dept_status,
                personality_status,
                madni_task_assessment,
                survey_id
        ) UNPIVOT ( star
            FOR q
        IN ( performance AS 'PERFORMANCE',
             teacher_behaviour AS 'TEACHER_BEHAVIOUR',
             syllabus AS 'SYLLABUS',
             shift_incharge_behaviour AS 'SHIFT_INCHARGE_BEHAVIOUR',
             punctuality AS 'PUNCTUALITY',
             teaching_status AS 'TEACHING_STATUS',
             voice_clarity_class AS 'VOICE_CLARITY_CLASS',
             daily_class AS 'DAILY_CLASS',
             exams_status AS 'EXAMS_STATUS',
             mtm_helpline_status AS 'MTM_HELPLINE_STATUS',
             fee_dept_status AS 'FEE_DEPT_STATUS',
             personality_status AS 'PERSONALITY_STATUS',
             madni_task_assessment AS 'MADNI_TASK_ASSESSMENT' ) )
    ORDER BY
        1,
        2
)
SELECT
    *
FROM
    survey PIVOT (
        COUNT ( student_id )
        FOR star
        IN ( 1, 2, 3, 4, 5 )
    )
ORDER BY
    q;

I want such desire Result as below is the picture.Total column is the sum of total records of my table

在此处输入图像描述

标签: sqloracleoracle11goracle-sqldeveloper

解决方案


推荐阅读