0

I have the following problem which could (I am hoping) have a standard solution.
I have a java application that interacts with the database and builds dynamically SQL strings.
So far I guess usual stuff.
Occusionally I need to have a sorting on the data and I use ORDER By. So far clear I guess.
Problem: I sometimes need to sort on a column that does not have the actual data but a short string that is a key to the actual data.
I mean:
SELECT FROM MYTable WHERE MyTable.col1 = 'A' ORDER BY MyTable.col2 ASC
And col2 has values: AB, BY, CY which return sorted but are useless to me as these are keys to the actual values from a properties file and so the end result is not sorted.
What is the best way to solve this in a manner consistent with the way it currently works?

8
  • Can you clarify what exactly is the end result in your case? Do you display this data in some grid, or something (and how)? Commented Oct 19, 2012 at 13:31
  • How many lines are concerned? I few => do it with SortedMap<K,V> if a lot, create a temp table in SQL from properties file Commented Oct 19, 2012 at 13:38
  • @Less:Yes they are displayed in a table by mapping to the values Commented Oct 19, 2012 at 13:38
  • How would I use a SortedMap? Commented Oct 19, 2012 at 13:39
  • For starters, you could check the docs to see if this fits your needs. Again, it depends on the situation you got going on over there, is it a lot of data, is it not (as Aubin said)... Commented Oct 19, 2012 at 13:43

1 Answer 1

5

If the data is not in the DB, then your choice is to either sort it in Java, or first insert the data from the property file into a temporary table and then join to that table and do the ORDER BY in SQL. If this is an operation that happens frequently, you might consider putting the properties into a permanent table.

Sign up to request clarification or add additional context in comments.

2 Comments

1)Is the creation of a temporary table supported by all database?2)I assume this can also be done from a java Statement, right?
It is supported in Oracle, MySql, SQL Server, Sybase. I'm not sure about any others, you could use a permanent table if you need to -- just key it by a unique id to prevent process clashes. Yes, you can do the creation of the temp table and the insert of the data using JDBC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.