我已经挑选出在不同难度级别的挑战中获得满分的人。但是,该问题指出,要查询hacker_id和不止一次获得满分的人员的姓名。我遇到COUNT个问题。我试图计算出现在表格中的名字的出现频率,但是我不允许这样做。我怀疑GROUPBY语法有问题。有人可以帮我吗?
以前的代码
Select s.challenge_id,s.hacker_id,h.name,s.submission_id,c.difficulty_level,s.score
FROM (((Hackers AS h JOIN Submission AS s ON h.hacker_id=s.hacker_id)JOIN Challenges AS c ON c.challenge_id=s.challenge_id)JOIN Difficulty AS d ON d.difficulty_level=c.difficulty_level)
WHERE c.difficulty_level=d.difficulty_level and s.score=d.score
结果
challenge_id | hacker_id | name |submission_id |difficulty_level |score
71055 86870 Todd 94613 2 30
66730 90411 Joe 97397 6 100
71055 90411 Joe 97431 2 30
问题
Select g.hacker_id,g.name,COUNT(g.name)
FROM (Select s.challenge_id,s.hacker_id,h.name,s.submission_id,c.difficulty_level,s.score
FROM (((Hackers AS h JOIN Submission AS s ON h.hacker_id=s.hacker_id)JOIN Challenges AS c ON c.challenge_id=s.challenge_id)JOIN Difficulty AS d ON d.difficulty_level=c.difficulty_level)
WHERE c.difficulty_level=d.difficulty_level and s.score=d.score) AS g
WHERE COUNT(g.name)>1
GROUBY g.hacker_id,g.name;
答案 0 :(得分:1)
如果您需要第一个查询中多次出现的每个名称的COUNT
,则可以使用以下内容
SELECT hacker_id, name, COUNT(name)
FROM
(
Select s.challenge_id,s.hacker_id,h.name,s.submission_id,c.difficulty_level,s.score
FROM (((Hackers AS h JOIN Submission AS s ON h.hacker_id=s.hacker_id)JOIN Challenges AS c ON c.challenge_id=s.challenge_id)JOIN Difficulty AS d ON d.difficulty_level=c.difficulty_level)
WHERE c.difficulty_level=d.difficulty_level and s.score=d.score
) AS T
GROUP BY hacker_id, name
HAVING COUNT(name) > 1
HAVING
用于过滤汇总结果