使用多个聚合的SQL Advanced Join

时间:2018-08-09 10:49:09

标签: mysql sql

嗨,我正在尝试使用以下链接中的sql解决问题

https://www.hackerrank.com/challenges/15-days-of-learning-sql/problem

我编写了以下sql查询,并得到了不正确的result.please哪里出错了请

SELECT s.submission_date,COUNT(DISTINCT(s.hacker_id)),s1.hacker_id, h.name
FROM  Hackers h INNER JOIN Submissions s ON (h.hacker_id = s.hacker_id)  
                INNER JOIN Submissions s1 ON s1.hacker_id = s.hacker_id
Where s1.hacker_id = (
SELECT w.hacker_id from 
                      (
                      SELECT hacker_id, COUNT(hacker_id)from Submissions s
                      GROUP BY s.submission_date,hacker_id
                      ORDER BY COUNT(hacker_id) desc, hacker_id ASC  
                      limit 1 
                      ) w
)
GROUP BY s.submission_date,s1.hacker_id,h.name
ORDER BY s.submission_date ASC;

实际输出为

2016-03-03 1 80682 Deborah 
2016-03-04 1 80682 Deborah 
2016-03-07 1 80682 Deborah 
2016-03-14 1 80682 Deborah 

示例输出为

2016-03-01 4 20703 Angela
2016-03-02 2 79722 Michael
2016-03-03 2 20703 Angela
2016-03-04 2 20703 Angela

如果可能,请建议使用相同代码而不是新代码进行修改。 谢谢

0 个答案:

没有答案