Question:
Julia conducted a 15 days of learning SQL contest. The start date of the contest was March 01, 2016 and the end date was March 15, 2016.
Write a query to print total number of unique hackers who made at least submission each day (starting on the first day of the contest), and find the
hacker_id
and name of the hacker who made maximum number of submissions each day. If more than one such hacker has a maximum number of submissions, print the lowesthacker_id
. The query should print this information for each day of the contest, sorted by the date.Input Format
The following tables hold contest data:
Hackers
: Thehacker_id
is the id of the hacker, andname
is the name of the hacker.Submissions
: Thesubmission_date
is the date of the submission,submission_id
is the id of the submission,hacker_id
is the id of the hacker who made the submission, andscore
is the score of the submission.Sample Input
For the following sample input, assume that the end date of the contest was March 06, 2016.Hackers Table
Submissions TableSample Output
2016-03-01 4 20703 Angela 2016-03-02 2 79722 Michael 2016-03-03 2 20703 Angela 2016-03-04 2 20703 Angela 2016-03-05 1 36396 Frank 2016-03-06 1 20703 Angela
My query so far is following:
SELECT DISTINCT
@s := submission_date,
COUNT(DISTINCT hacker_id),
@h := (SELECT hacker_id
FROM (SELECT DISTINCT
hacker_id,
@ch := COUNT(hacker_id)
FROM Submissions
WHERE submission_date=@s
ORDER BY @ch
GROUP BY hacker_id DESC) x
LIMIT 1),
@n:=(SELECT name FROM Hackers WHERE hacker_id=@h)
FROM
Submissions
WHERE 1;
Well this query fails 'SQL sytax error'{MYSQL}, however without GROUP BY
this query is rejected as only-full-group-by mode is activated.
Error Message is:
ERROR 1064 (42000) at line 5: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY hacker_id DESC) x LIMIT 1),@n:=(SELECT name FROM Hackers WHERE hacker_i' at line 1
CodePudding user response:
You didn't mention the column name after WHERE.
Try this:
SELECT <your query> FROM Submissions WHERE <column name> = 1;
I think this will be helpful for you.
CodePudding user response:
Though this does not provide correct answer but changing query as suggested by Mr Daniyal solved Syntax issue. Following is final query:
SELECT DISTINCT
@s := submission_date,
COUNT(DISTINCT hacker_id),
@h := (SELECT hacker_id
FROM Submissions
WHERE submission_date=@s
GROUP BY hacker_id
ORDER BY COUNT(hacker_id)
DESC LIMIT 1),
@n:=(SELECT name FROM Hackers WHERE hacker_id=@h)
FROM
Submissions
WHERE 1 GROUP BY submission_date;