title: Each GROUP BY expression must contain at least one column that is not an outer reference
body: <p>What am I doing wrong here? I am getting this error on:</p>
  
  <pre><code>SELECT LEFT(SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%', batchinfo.datapath), 8000), 
              PATINDEX('%[^0-9]%', SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%', 
              batchinfo.datapath), 8000))-1),
              qvalues.name,
              qvalues.compound,
              qvalues.rid
  FROM batchinfo JOIN qvalues ON batchinfo.rowid=qvalues.rowid
  WHERE LEN(datapath)&gt;4
  GROUP BY 1,2,3
  HAVING rid!=MAX(rid)
  </code></pre>
  
  <p>I would like to group by the first, second, and third columns having the max rid.</p>
  
  <p>It works fine without the group by and having.</p>