title: SQL doesn't work on a large sample
body: <p>I am trying to solve a challenge and came up with the solution. The solution which I have written works for small dataset but it doesn't seem to work fine for larger ones. Could someone help me where am I doing wrong?</p>
  
  <p>I am facing trouble with calculating the unique users for each day (second column in the output). The rest of the logic works fine.</p>
  
  <blockquote>
    <p>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.</p>
    
    <p>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 lowest hacker_id. The query should print this information for each day of the contest, sorted by the date.</p>
    
    <p>Input Format</p>
    
    <p>The following tables hold contest data:</p>
    
    <p>Hackers: The hacker_id is the id of the hacker, and name is the name
    of the hacker.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/3aYIa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3aYIa.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>Submissions: The submission_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, and score is the score of the submission. </p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/E529Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E529Z.png" alt="enter image description here"></a></p>
  
  <p>Sample input</p>
  
  <p><a href="https://i.stack.imgur.com/fVYLr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fVYLr.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/ppdZR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ppdZR.png" alt="enter image description here"></a></p>
  
  <p>For the following sample input, assume that the end date of the contest was March 06, 2016.</p>
  
  <p>Hackers Table: Submissions Table: </p>
  
  <pre><code>**Explanation :-**
  </code></pre>
  
  <p>On March 01, 2016 hackers , , , and  made submissions. There are  unique hackers who made at least one submission each day. As each hacker made one submission,  is considered to be the hacker who made maximum number of submissions on this day. The name of the hacker is Angela.</p>
  
  <p>On March 02, 2016 hackers , , and  made submissions. Now  and  were the only ones to submit every day, so there are  unique hackers who made at least one submission each day.  made submissions, and name of the hacker is Michael.</p>
  
  <p>On March 03, 2016 hackers , , and  made submissions. Now  and  were the only ones, so there are  unique hackers who made at least one submission each day. As each hacker made one submission so  is considered to be the hacker who made maximum number of submissions on this day. The name of the hacker is Angela.</p>
  
  <p>On March 04, 2016 hackers , , , and  made submissions. Now  and  only submitted each day, so there are  unique hackers who made at least one submission each day. As each hacker made one submission so  is considered to be the hacker who made maximum number of submissions on this day. The name of the hacker is Angela.</p>
  
  <p>On March 05, 2016 hackers , ,  and  made submissions. Now  only submitted each day, so there is only  unique hacker who made at least one submission each day.  made  submissions and name of the hacker is Frank.</p>
  
  <p>On March 06, 2016 only  made submission, so there is only  unique hacker who made at least one submission each day.  made  submission and name of the hacker is Angela.</p>
  
  <p>Sample Output</p>
  
  <pre><code>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
  
  Schema &amp; Data :-
  </code></pre>
  
  <p><a href="http://sqlfiddle.com/#!9/844928" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/844928</a></p>
  
  <pre><code>Solution :-
  
  
  SELECT A.submission_date, A.cnt, B.hacker_id, B.name 
    FROM
      (
          SELECT submission_date, COUNT( DISTINCT hacker_id ) AS cnt
            FROM submissions
           WHERE submission_date = '2016-03-01'
           GROUP BY submission_date 
          UNION ALL
          SELECT submission_date, COUNT( DISTINCT hacker_id )
            FROM
              (
                  SELECT DATEADD(day, 1, convert( date, A.submission_date ))  AS submission_date, A.hacker_id
                    FROM 
                      (
                         SELECT submission_date, hacker_id
                           FROM submissions
                         GROUP BY submission_date, hacker_id
                       ) A
                  INNER  JOIN  
                      (
                           SELECT DATEADD(day, -1, convert( date, submission_date )) AS new_submission_date, hacker_id
                             FROM submissions
                            GROUP BY DATEADD(day, -1, convert( date, submission_date )) , hacker_id
                       ) B
                ON A.submission_date = B.new_submission_date
               AND A.hacker_id = B.hacker_id  
              ) Z
          GROUP BY submission_date
      ) A
  INNER JOIN 
  (
      SELECT s.submission_date, s.hacker_id, h.name
        FROM
      (
          SELECT submission_date, hacker_id 
            FROM
          ( 
              SELECT submission_date, hacker_id,cnt, ROW_NUMBER() OVER ( PARTITION BY submission_date ORDER BY cnt DESC, hacker_id ) AS rn
                FROM 
              (
               SELECT submission_date, hacker_id, COUNT(*) AS cnt
                 FROM submissions
                GROUP BY submission_date, hacker_id
              ) Z
          ) Y
          WHERE rn = 1
      ) s
      INNER JOIN
      hackers h
      ON s.hacker_id = h.hacker_id
  ) B
  ON A.submission_date = B.submission_date
  ;
  </code></pre>