简体   繁体   English

MySQL中不同值的总和

[英]Sum for Distinct values in MySQL

I have three tables, the structure is listed as following. 我有三个表,结构如下所示。

This table (called contest_submissions ) stores the relationship of submissions and contests. 该表(称为contest_submissions )存储提交和竞赛的关系。

ContestID | SubmissionID

1           1000
1           1001
1           1002
1           1003

The second table (called submissions ) stores the detail information of a submission: 第二个表(称为submissions )存储submissions的详细信息:

SubmissionID | ProblemID | User | Score | Time

1000           1000        A      100     1000
1001           1000        A      40      1250
1002           1001        A      50      1500
1003           1001        B      20      1750

Another table (called contest_contestants ) is consisted of: 另一个表(称为contest_contestants )包括:

ContestID | User

1           A
1           B

I wrote the following SQL: 我写了以下SQL:

SELECT *, (
    SELECT SUM(score)
    FROM  contest_submissions cs
    NATURAL JOIN submissions
    WHERE user = cc.user
    AND SubmissionID = cs.SubmissionID
) AS TotalScore, (
    SELECT SUM(Time)
    FROM contest_submissions cs
    NATURAL JOIN submissions
    WHERE user = cc.user
    AND SubmissionID = cs.SubmissionID
) AS TotalTime
FROM contest_contestants cc
WHERE contestID = 1

I got following result (Suppose ContestID = 1 ): 我得到了以下结果(假设ContestID = 1 ):

contestID | User | Total Score | Total Time
1           A      190           3750
1           B      20            1750

where 190 = 100 + 40 + 50 . 其中190 = 100 + 40 + 50

However, I want to get following result: 但是,我希望获得以下结果:

contestID | User | Total Score | Total Time
1           A      150           2500
1           B      20            1750

where 150 = MAX(100, 40) + 50 , because 100 and 40 come from the same problem (with the same ProblemID ). 其中150 = MAX(100, 40) + 50 ProblemID150 = MAX(100, 40) + 50 ,因为10040来自同一问题(具有相同的ProblemID )。

What should I do? 我该怎么办?

BTW, I'm using MySQL. 顺便说一下,我正在使用MySQL。

you can try something like that: 你可以试试这样的东西:

select User, sum(MaxScore)
from
(
select User, ProblemID, max(Score) as MaxScore
from submissions
group by User, ProblemId
) as t
group by User

Hmmm. 嗯。 I think there is a way to do this with only one group by : 我认为有一种方法可以通过以下方式只对一group by

select s.user, sum(s.score)
from submissions s
where s.submissionId = (select s2.submissionId
                        from submissions s2
                        where s2.user = s.user and s2.ProblemId = s.ProblemId
                        order by s2.score desc
                        limit 1
                       )
group by s.user;

I offer this as a solution because with an index on submissions(user, ProblemId, score, submissionId) it should have somewhat better performance than the solutions with two aggregations. 我提供这个作为解决方案,因为submissions(user, ProblemId, score, submissionId)索引submissions(user, ProblemId, score, submissionId)它应该比具有两个聚合的解决方案有一些更好的性能。

You could use a nest query - the inner one to get the users' best answer for each problem and the outer one to sum them: 你可以使用一个嵌套查询 - 内部查询来获得用户对每个问题的最佳答案,而外部查询则将它们相加:

SELECT   user, SUM(score) AS total_score
FROM     (SELECT   user, problemid, MAX(score) AS score
          FROM     submission
          GROUP BY user, problemid) t
GROUP BY user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM