MySql - 动态逻辑计算器

时间:2015-08-19 20:14:11

标签: mysql mysql-workbench

请找表" MarkCompare"以下

SEMESTER - PAPER - TEACHER 1 - TEACHER 2
1        - ENG   - PASS      - PASS
1        - MATH  - PASS      - FAIL
2        - ENG   - PASS      - FAIL
2        - MATH  - FAIL      - FAIL

我想计算如下逻辑

No.Of.Times,其中两位老师​​给出相同的结果/出现的总和,其中第一位教师的教师结果不同

我正在写这样的查询

select count(*) from MarkCompare where teacher1=teacher2 where paper='ENG' / (select count(*) from MarkCompare where teacher1<>teacher2 where paper='ENG')
select count(*) from MarkCompare where teacher1=teacher2 where paper='MATH' / (select count(*) from MarkCompare where teacher1<>teacher2 where paper='MATH')

现在,将来论文数量可能会增加或减少......我无法找到适合任意数量论文的动态查询。

有没有办法在没有任何程序/功能的情况下执行此操作,只需查询

1 个答案:

答案 0 :(得分:4)

您可以使用GROUP BY子句聚合表中列出的每个paper的结果。

SELECT paper,
       SUM(CASE WHEN teacher1 = teacher2 THEN 1 ELSE 0 END) AS AgreeCount,
       SUM(CASE WHEN teacher1 <> teacher2 THEN 1 ELSE 0 END) AS DisagreeCount
    FROM MarkCompare
    GROUP BY paper;