Question

I need some help with SQL query creation. I have data like

EN 771-2:2011
EN 197-1:2011
EN 295-1:2013
771-1:2011 
EN 54-24:2008
EN 492:2012
EN 54-25: 2008
EN 331:1998
EN 534:2006+A1:2010
EN 588-2:2001
EN 179:2008
EN 598:2007+A1:2009
EN 621:2009
EN 682: 2002

Is possible create ORDER BY causule, when result of ordering will be:

EN 54-24:2008
EN 54-25: 2008
EN 179:2008
EN 197-1:2011
EN 295-1:2013
EN 331:1998
EN 492:2012
EN 534:2006+A1:2010
EN 588-2:2001
EN 598:2007+A1:2009
EN 621:2009
EN 682: 2002
771-1:2011 
EN 771-2:2011

respectively, I need order, which will be depended on part of substring:

EN 54-24:2008, EN 54-25: 2008, 771-1:2011 , EN 771-2:2011

Bold characters should have highest priority and italic characters should have lower. Is possible to create "ORDER BY" causule for results something like this? I know about substring function, but she give different results to me.

Thank you for help.

Was it helpful?

Solution

You'd have to formally define via regexp what exactly is "bold" or "italic".

If you assume that the first group of digits is the first variable to order on and the second is the second:

ORDER BY substring(col,'\d+')::int, substring(col,'\d+[^\d]+(\d+)')::int;

Or alternatively, if your definition is that the first number is the digits following the first space and the second is the ones following a dash:

ORDER BY substring(col,' (\d+)')::int, substring(col,'-(\d+)')::int;

Of course you should first debug these buy running select substring(...).

OTHER TIPS

Yes, Try this. Assuming that first 2 characters are always EN, then followed by 3 numbers and a -

select column_name from table_name order by   
substring(column_name,3,3),substring(column_name,7,length(column_name)-7)

Create an additional column in the table that contains the values in their desired lexicographic order. Write code (or other table driven process) to set the value of this column when you enter new values in the table. Add an index for performance. Allow the user to override the system proposed value.

Use this table in the ORDER BY clause to arrange the rows into the desired sequence.

Otherwise, you will build an arbitrarily complex rule set that will never cater for all exceptions and maintenance will eventually swamp you with complexity.

You can use regular expressions, or something similar, to extract the proposed ordering strings from the original name.

You have a string of the format: " :".

You can do the ordering by judicious use of substring_index(). The idea is to convert the to a number, first by extracting the value and then converting it to a number. This gets you very close to what you want:

order by substring_index(substring_index(causule, ':', 1), -1) + 0

The problem is the part after the hyphen. Rows with the same value of "" are all equivalent. To distinguish them, pull out the part of the string after the space:

order by substring_index(substring_index(causule, ':', 1), -1) + 0,
         substring_index(substring_index(causule, ':', 1), -1)

Yes, substring as you say, but then you have to cast it to integer so that a numeric order is being used (which seems to be what you want).

Untested, try this in the order by:

cast(substring(YOURFIELDNAME from '\s*\d+-') as integer)

It uses a regular expression:

\s for a space * to make it optional \d for digits + to pick one or more digits - the minus sign

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top