views:

74

answers:

6

The following SQL statement works in MySQL but not with Oracle:

SELECT *, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_A

Oracle complaint: "FROM keyword not found where expected"

+3  A: 

That's because Oracle requires you to define all the columns not wrapped in an aggregate function (MIN, MAX, COUNT, etc). SQL Server would return a similar error. MySQL's behavior is documented here.

Because your query is using SELECT *, I can't re-write it properly for you. But I also can't guarantee a syntactically correct version would return the same results as you see on MySQL either. Grouping by the same column you want the MAX is quite odd...

OMG Ponies
Actually all DBMS except MySQL will refuse to run such a query. MySQL simply returns indeterminate results
a_horse_with_no_name
actually the statement was incorrect, we were not grouping by COLUMN_A but another column instead. actually what we want is this<code>SELECT *, MAX(COLUMN_A) FROM table_xyz WHERE COLUMN_A <= 100 GROUP BY COLUMN_Bthis works but gives us only column A and BSELECT COLUMN_B, MAX(COLUMN_A) FROM table_xyz WHERE COLUMN_A <= 100 GROUP BY COLUMN_Bwhat we want is this, but it doesn't work (group by error)SELECT COLUMN_B, COLUMN_C .... COLUMN_X, MAX(COLUMN_A) FROM table_xyz WHERE COLUMN_A <= 100 GROUP BY COLUMN_B</code>
Dave
@a_horse_with_no_name: No, SQLite supports the same "hidden column" functionality that MySQL supports. But any *real* database, yes - you're correct. :)
OMG Ponies
@Dave: Update your question with example data, output from `DESC table_xyz`, and expected output.
OMG Ponies
+1  A: 

If you want the max() for column_a you don't need the group by at all:

SELECT MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
a_horse_with_no_name
A: 

Multiple problems. Your GROUP BY clause is backwards. You need to define your GROUP BY by the columns in the *. Also what OMG Ponies said before.

Dave
+1  A: 

In addition to what everyone else is saying, Oracle does not allow mixing * with explicit column definitions in queries:

SQL> select *, table_name from user_tables;
select *, table_name from user_tables
        *
ERROR at line 1:
ORA-00923: FROM keyword not found where expected

Oracle hasn't even looked at the fact that you are trying to get columns outside of those included in the group by clause. Which as others have stated, Oracle will not do.

Shannon Severance
A: 

actually the statement was incorrect, we were not grouping by COLUMN_A but another column instead. actually what we want is this

SELECT *, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_B

this works but gives us only column A and B

SELECT COLUMN_B, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_B

what we want is this, but it doesn't work (group by error)

SELECT COLUMN_B, COLUMN_C .... COLUMN_X, MAX(COLUMN_A) 
  FROM table_xyz 
 WHERE COLUMN_A <= 100 
GROUP BY COLUMN_B
Dave
See my other comment about what to update your question with. I won't, but others can downvote this as it's not an answer. You can delete it by clicking the delete hyperlink to the left of your username.
OMG Ponies
+1  A: 

This doesn't answer your MAX issue, but the only way to follow a '*' with other columns is if you use an explicit reference to a table alias - e.g.

 SELECT e.*, zip_code
 FROM  addresses a,
       employees e
 WHERE e.addressId = a.Id

For the MAX value, you will either need to group by all other columns, or look into analytic functions (plenty of previous answers on Stack Overflow).

JulesLt