postgresql - extract the max value from string in sql -
i work postgresql , want extract max value query:
select max (num_ordre_decision) decision
the problem num_ordre_decision
contains kind of data:
'4/35/677' '4/35/1001'
the type of column string. when run query have max value:
'4/35/1001'
which false.
i want compare 677
1001
in order have max value '4/35/677'
in previous case.
i think should use split in query.
update
i tried query:
select max(split_part(num_ordre_decision, '/', 3)) decision;
i have value: '99'
when try run query
select split_part(num_ordre_decision, '/', 3) decision;
i result
'' '' '677' '1001' '99'
this result ''
appears because real value in table not contain separator '/'
. think max(split_part(num_ordre_decision, '/', 3)
makes sum meaning makes 9+9
. correct result should 1001
so must compared numbers.
updated :
i want use query in project
i have function :
public list getmaxnumorder (){ string query= "select max(nullif(split_part(num_ordre_decision, '/', 3), '')::int) decision"; sqlquery sqlquery = this.getsession().createsqlquery(query); return sqlquery.list(); }
but when have error :
org.hibernate.queryexception: not named parameters have been set: [:int] [select max(nullif(split_part(num_ordre_decision, '/', 3), '')::int) decision] @ org.hibernate.impl.abstractqueryimpl.verifyparameters(abstractqueryimpl.java:339) @ org.hibernate.impl.sqlqueryimpl.verifyparameters(sqlqueryimpl.java:228)
i think should use : query.setparameter
i not know how use it
you can max value as:
select max(split_part(num_ordre_decision, '/', 3)) decision;
to overall max, might want:
select num_ordre_decision decision order split_part(num_ordre_decision, '/', 3) desc limit 1;
Comments
Post a Comment