Skip to content
Advertisement

CSVJDBC – Interpreting Strings Instead of Integers in Aggregate Functions

I am using the CSVJDBC driver for retrieving results from CSV files. All record fields are interpreted as Strings. How can I utilise the MAX aggregate function in order to obtain the maximim Integer of a column? As far as I know, csvjdbc does not support casting.

Consider this sample file:

I use the following code snippet:

When I execute the query

I get as expected:

But when I want the maximum sequenceNumber, which is 19 with this

I get 9 as a result:

It works for the decimalNumber and it also works for the text. It does not work for testNumber, as csvjdbs returns the lexicographic largest value instead of the Integer value. Is there a possibility to solve this directly or to I need to fetch all records and select the maximum value with Java?

Basic Solution:

This is my basic solution, which needs to fetch all the numbers first:

Is there a more elegant way?

Solution based on Joop Eggen

Advertisement

Answer

You should better specify the column types as it seems the first column is taken as String, where "9" > "10".

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement