Each GROUP BY expression must contain at least one column that is not an outer reference

前端 未结 7 1753
-上瘾入骨i
-上瘾入骨i 2020-12-09 07:50

What am I doing wrong here? I am getting this error on:

SELECT LEFT(SUBSTRING(batchinfo.datapath, PATINDEX(\'%[0-9][0-9][0-9]%\', batchinfo.datapath), 8000),         


        
7条回答
  •  时光说笑
    2020-12-09 08:43

    Well, as it was said before, you can't GROUP by literals, I think that you are confused cause you can ORDER by 1, 2, 3. When you use functions as your columns, you need to GROUP by the same expression. Besides, the HAVING clause is wrong, you can only use what is in the agreggations. In this case, your query should be like this:

    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) MaxRid
    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
    

提交回复
热议问题