Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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),              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)>4 GROUP BY 1,2,3 HAVING rid!=MAX(rid) 

I would like to group by the first, second, and third columns having the max rid.

It works fine without the group by and having.

like image 842
JOE SKEET Avatar asked Dec 15 '10 18:12

JOE SKEET


People also ask

What is outer reference in SQL?

Description of an outer reference An outer reference is a column name that does not refer to any of the columns in any of the tables in the FROM clause of the subquery. Instead, the column name refers to a column of a table specified in the FROM clause of the main query.

How do I group by in SQL Server?

The SQL GROUP BY StatementThe GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

What is aggregate function in SQL Server?

An aggregate function performs a calculation on a set of values, and returns a single value. Except for COUNT(*) , aggregate functions ignore null values. Aggregate functions are often used with the GROUP BY clause of the SELECT statement. All aggregate functions are deterministic.


2 Answers

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 
like image 75
Cade Roux Avatar answered Sep 28 '22 12:09

Cade Roux


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 
like image 44
Lamak Avatar answered Sep 28 '22 10:09

Lamak