To start with you can’t do this:
having rid!=MAX(rid)
The HAVING clause can only contain things which are attributes of the aggregate groups.
In addition, 1, 2, 3
is not valid in GROUP BY in SQL Server – I think that’s only valid in ORDER BY.
Can you explain why this isn’t what you are looking for:
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, MAX(qvalues.rid) from batchinfo join qvalues on batchinfo.rowid=qvalues.rowid where LEN(datapath)>4 group by 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