1

为了使大的 sql 结构简单:我有下一个结构:

id    level    answercorrect    points
1     1        0                0
2     1        1                60
3     1        1                80
4     1        1                77
5     1        1                92
6     1        0                0
7     2        0                0
8     2        0                0
9     2        0                0
10    2        1                80
11    2        0                0
12    2        0                0

我现在想显示两件事:

1)完成了多少级(这是至少一半的问题正确)

2)你得分最高的级别。

现在我可以去手动查询每个级别,然后比较它们,然后输出它们。

SELECT (*) FROM QUESTIONS WHERE LEVEL = level AND ANSWERCORRECT = 1

但是有没有办法更快地做到这一点?在我的路上完成这似乎是一大堆代码..?

4

1 回答 1

2

这将向您显示每个级别,包括答案计数和正确答案计数。

select level, 
    count(*) as totalquestions, 
    sum (answercorrect) as correctanswers,
    sum (points) as totalpoints
from yourtable
group by level
having sum (answercorrect) >= (count(*)/2)
order by sum (points) desc

或者

select level,  
    count(*) as totalquestions,  
    sum (answercorrect) as correctanswers, 
    sum (points) as totalpoints,
    case when sum (answercorrect) >= (count(*)/2) then 1 else 0 end as completed
from yourtable 
group by level 
order by sum (points) desc 

存储分数和正确性似乎是多余的,如果 points = 0 表示不正确,而 points>0 表示正确?

于 2012-10-10T08:06:45.967 回答