SQL and Jython - selecting MAX value in COLUMN

says :

I'm trying to get the max value from a column in a table.

SQL_query = """
            SELECT 
            MAX(COLUMN_NAME)
            FROM 
            TABLE_NAME
            """ 

stmt = conn.createStatement()
resultSet = stmt.executeQuery(SQL_query)


if resultSet.next():
    print resultSet.getString('COLUMN_NAME')

When I have MAX in the SQL query I get an error:

Traceback ...
... print resultSet.getString('COLUMN_NAME')
    java.sql.SQLException: Invalid column name

But when I leave out MAX in the SQL query I get the first value in the column, which also corresponds to the MIN value. I'm pretty new to Jython/Java so I'm not really sure why this is happening or how to resolve the issue. Any help would be much appreciated!

GMB :

Just alias the column name:

SELECT MAX(COLUMN_NAME) MAX_COLUMN_NAME
FROM TABLE_NAME

You can then refer to the alias in the resultset:

print resultSet.getString('MAX_COLUMN_NAME')

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23248&siteId=1