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!
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')