title: mysql query is returning incorrect values
body: <p>I have the following data,</p>
  <pre><code>started ended   theDate     theYear themonth    growth  division    teams   status   location
  2         0      8/31/2019  2019    8            2    Unknown       Team A  ACTIVE   Town A
  1         0      5/31/1996  1996    5            1    Unknown       Team B  ACTIVE   Town A
  1         0      8/31/2014  2014    8            1    Unknown       Team B  ACTIVE   Town B
  1         0      1/31/1996  1996    1            1    Unknown       Team B  ACTIVE   Town C
  1         0      7/31/2004  1985    7            1    Unknown       Team C  ACTIVE   Town E
  1         0      7/31/1985  1985    7            1    Unknown       Team B  ACTIVE   Town A
  1         0      5/31/2019  2019    5            1    Unknown       Team A  ACTIVE   Town F
  </code></pre>
  <p>The started column shows the employees that have joined on that particular date. The growth column is started - ended where ended is the number of employees that left.</p>
  <p>I have the following query which will extract the data correctly as long as i specifiy the variables.</p>
  <pre><code>set @theYear = 2019 ;
  set @team = 'Team A' ;
  
  
  SELECT 
      t1.growth,
      SUM(t2.growth) AS Emp_Count,
      CASE
          WHEN t1.theYear IS NULL THEN t1.theYear
          ELSE t1.theYear
      END AS theYear,
       t1.team,
      t1.location,
      t1.division,
      t1.status,
      CASE
          WHEN t1.Month = 1 THEN 'JAN'
          WHEN t1.Month = 2 THEN 'FEB'
          WHEN t1.Month = 3 THEN 'MAR'
          WHEN t1.Month = 4 THEN 'APR'
          WHEN t1.Month = 5 THEN 'MAY'
          WHEN t1.Month = 6 THEN 'JUN'
          WHEN t1.Month = 7 THEN 'JUL'
          WHEN t1.Month = 8 THEN 'AUG'
          WHEN t1.Month = 9 THEN 'SEP'
          WHEN t1.Month = 10 THEN 'OCT'
          WHEN t1.Month = 11 THEN 'NOV'
          WHEN t1.Month = 12 THEN 'DEC'
      END AS myMONTH
  FROM
      (SELECT 
          CASE
                  WHEN r.growth IS NOT NULL THEN r.growth
                  WHEN r.growth IS NULL THEN 0
              END AS growth,
              r.theYear,
              r.team,
              r.division,
              r.location,
              t.mon_num AS Month,
              r.status
      FROM
          Reports r
      RIGHT JOIN (SELECT 1 mon_num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) t ON t.mon_num = r.themonth
          AND r.theYear =  (select @theYear)
          AND r.team = (select @team)
      GROUP BY r.growth , Month
      ORDER BY t.mon_num ASC) AS t1
          JOIN
      (SELECT 
          CASE
                  WHEN r2.growth IS NOT NULL THEN r2.growth
                  WHEN r2.growth IS NULL THEN 0
              END AS growth,
              r2.theYear,
              r2.team,
              r2.division,
              r2.location,
              t.mon_num AS Month,
              r2.status
      FROM
          Reports r2
      RIGHT JOIN (SELECT 1 mon_num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) t ON t.mon_num = r2.themonth
          AND r2.theYear = (select @theYear)
          AND r2.team = (select @team)
      GROUP BY r2.growth , Month
      ORDER BY t.mon_num ASC) AS t2 ON t1.Month &gt;= t2.Month
  GROUP BY t1.Month;
  </code></pre>
  <p>As you can see below the query will list the months i.e Jan,feb etc along with the selected data along with the incremental count.</p>
  <p><a href="https://i.stack.imgur.com/NzAdf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NzAdf.png" alt="enter image description here" /></a></p>
  <p>However if I want to use the query without the variables i.e. get all data without any conditions/filters the result i get is incorrect. First off it should start with the year 1985 as i do have the year 1985 in my dataset.</p>
  <p><a href="https://i.stack.imgur.com/2V7Ce.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2V7Ce.png" alt="enter image description here" /></a></p>