sql - 计算所有表记录计数的麻烦
问题描述
我已经尝试使用子查询编写 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
解决方案
推荐阅读
- sql - 如何仅在表 b 中选择记录
- python - AWS SAM 未与 Python 3.7 一起安装
- python-3.x - 如何使用 boto3 过滤器获取布尔值
- javascript - AngularJS ng-repeat 使用新的 API 调用刷新
- python - 仅当它们存在于 python 中时才将命令行选项添加到子进程
- html - Bootstrap Navbar Dropdowns 每次打开第一个下拉菜单
- c# - 在另一个线程上继续时异步
- c# - 读取和写入具有命名空间的 XML 文件,而无需遍历每个元素
- regex - url中的正则表达式。django 2.0中的py
- matlab - 在 MATLAB 中平均大向量的误差