现有一考评表,结构如下
---------------------------------------------------------------
officeId examType dataId examCode score date
部门id 考评类型 考评数据Id 考评编码 得分 考评时间
---------------------------------------------------------------
1 a 1 a1 4 0
1 a 1 a2 4 0
1 a 1 a3 3 0
1 a 2 a1 5 0
1 a 2 a2 4 0
1 a 2 a3 5 01 b 1 b1 5 2008-06-01
1 b 1 b2 3 2008-06-01
1 b 1 b3 3 2008-06-01
1 b 2 b1 2 2008-06-01
1 b 2 b2 4 2008-06-01
1 b 2 b3 5 2008-06-01
---------------------------------------------------------------
对一个部门的考核总分结果为(仅以sql语句示例)
sum(case when examType='a' then score end) / count(case when examType='a' then dataId end)
+
sum(case when examType='b' then score end) / count(case when examType='b' then dataId end)即
总分 =
考评类型为a的总分 / 考评类型为a的数量
+
考评类型为b的总分 / 考评类型为b的数量现在的问题是,有没有办法,一次统计出各部门考评总分,并排序? 且不用子查询的方法。以前想过用 avg(score) 来进行排序,后来发现实际上行不通,例如某部门只有 a的情况下排序根本不正确。多谢诸位高手了,
---------------------------------------------------------------
officeId examType dataId examCode score date
部门id 考评类型 考评数据Id 考评编码 得分 考评时间
---------------------------------------------------------------
1 a 1 a1 4 0
1 a 1 a2 4 0
1 a 1 a3 3 0
1 a 2 a1 5 0
1 a 2 a2 4 0
1 a 2 a3 5 01 b 1 b1 5 2008-06-01
1 b 1 b2 3 2008-06-01
1 b 1 b3 3 2008-06-01
1 b 2 b1 2 2008-06-01
1 b 2 b2 4 2008-06-01
1 b 2 b3 5 2008-06-01
---------------------------------------------------------------
对一个部门的考核总分结果为(仅以sql语句示例)
sum(case when examType='a' then score end) / count(case when examType='a' then dataId end)
+
sum(case when examType='b' then score end) / count(case when examType='b' then dataId end)即
总分 =
考评类型为a的总分 / 考评类型为a的数量
+
考评类型为b的总分 / 考评类型为b的数量现在的问题是,有没有办法,一次统计出各部门考评总分,并排序? 且不用子查询的方法。以前想过用 avg(score) 来进行排序,后来发现实际上行不通,例如某部门只有 a的情况下排序根本不正确。多谢诸位高手了,
+
sum(if(examType='b',score,0)) / if(sum(if(examType='b',1,0))=0,1,sum(if(examType='b',1,0))).
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
考评类型为a的总分 / 考评类型为a不重复dataId的数量
+
考评类型为b的总分 / 考评类型为b不重复dataId的数量
if(sum(if(examType='a',1,0))=0,1,sum(if(examType='a',1,0)))
改如何实现dataId的不重复呢?
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
所以我前面一直用 distinct 来过滤我是这样写的select officeId,
(
sum(case when examType=\'a\' then score end)/count(distinct case when examType=\'a\' then dataId end)
+
sum(case when examType=\'b\' then score end)/count(distinct case when examType=\'b\' then dataId end)
) as scorefrom table group bu officeId oreder by score desc
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
我把score*10000以后计算,结果就一直了。