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

前端 未结 7 1787
-上瘾入骨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:50

    When you're using GROUP BY, you need to also use aggregate functions for the columns not inside your group by clause.

    I don't know exactly what you're trying to do, but I guess this would work:

    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
    having
        rid!=MAX(rid)
    

    Edit: What I'm trying to do here is a group by with all fields but rid. If that's not what you want, what you need to do in order to have a valid SQL statement is adding an aggregate function call for each removed group by field...

提交回复
热议问题