Postgresql按空间排序
假设我有一个只有一列的表,即:Postgresql按空间排序,sql,ruby-on-rails,postgresql,Sql,Ruby On Rails,Postgresql,假设我有一个只有一列的表,即: 5 absolute 5.0 5.1 last 50 5 elite edge 我需要使用postgresql方法来订购: 5 absolute 5 elite 5.0 5.1 50 edge last 但如果我使用经典的按列排序ASC,我会得到: 50 5.0 5.1 5 absolute 5 elite edge last 有很多工具,比如substring或using,但我不明白它们是如何工作的 我需要做什么?这应该可以做到: order by reg
5 absolute
5.0
5.1
last
50
5 elite
edge
我需要使用postgresql方法来订购:
5 absolute
5 elite
5.0
5.1
50
edge
last
但如果我使用经典的按列排序ASC,我会得到:
50
5.0
5.1
5 absolute
5 elite
edge
last
有很多工具,比如substring或using,但我不明白它们是如何工作的
我需要做什么?这应该可以做到:
order by regexp_replace(the_column, '[^0-9\.]', '', 'g')::numeric DESC
它删除所有非数字字符,只留下数字和数字。然后将其转换为数字。然后使用该数字进行降序排序
唯一的问题是,5.0将在5.1之后进行排序
如果需要考虑没有任何数字的值,请执行以下操作:
order by
case
when regexp_replace(the_column, '[^0-9\.]', '', 'g') <>
then regexp_replace(the_column, '[^0-9\.]', '', 'g')::numeric
end DESC NULLS LAST,
the_column DESC -- to sort the "NULL" values alphabetically
with clean_data as (
select the_column,
regexp_replace(the_column, '[^0-9\.]', '', 'g') as clean_column,
....
from ...
)
select *
from clean_data
order by case
when clean_column <> '' then clean_column::numeric
end desc nulls last,
the_column desc;
with cte as (
select col1, regexp_split_to_array(col1, ' ') as d
from Table1
)
select col1
from cte
order by
d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' desc,
case
when d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' then
d[1]::numeric
end,
d[2]
如果不想重复正则表达式,可以执行以下操作:
order by
case
when regexp_replace(the_column, '[^0-9\.]', '', 'g') <>
then regexp_replace(the_column, '[^0-9\.]', '', 'g')::numeric
end DESC NULLS LAST,
the_column DESC -- to sort the "NULL" values alphabetically
with clean_data as (
select the_column,
regexp_replace(the_column, '[^0-9\.]', '', 'g') as clean_column,
....
from ...
)
select *
from clean_data
order by case
when clean_column <> '' then clean_column::numeric
end desc nulls last,
the_column desc;
with cte as (
select col1, regexp_split_to_array(col1, ' ') as d
from Table1
)
select col1
from cte
order by
d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' desc,
case
when d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' then
d[1]::numeric
end,
d[2]
不知道,可能是这样的:
order by
case
when regexp_replace(the_column, '[^0-9\.]', '', 'g') <>
then regexp_replace(the_column, '[^0-9\.]', '', 'g')::numeric
end DESC NULLS LAST,
the_column DESC -- to sort the "NULL" values alphabetically
with clean_data as (
select the_column,
regexp_replace(the_column, '[^0-9\.]', '', 'g') as clean_column,
....
from ...
)
select *
from clean_data
order by case
when clean_column <> '' then clean_column::numeric
end desc nulls last,
the_column desc;
with cte as (
select col1, regexp_split_to_array(col1, ' ') as d
from Table1
)
select col1
from cte
order by
d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' desc,
case
when d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' then
d[1]::numeric
end,
d[2]
这一个将字符串按空格拆分为数组,将第一个条目转换为数字,并按此数字和剩余字符串对结果进行排序这似乎是正确的,但我忘记了所有字符串只能有[a-z]值。因此,类型numeric:的输入语法无效。请看更新的问题谢谢,它是有效的,但我认为这是非常困难的postgres@asiniy:您始终可以编写一个实现上述逻辑的函数,然后在查询中使用该函数,该函数将通过获取\排序\值\列HM,减少查询从某些\表顺序中选择*的次数。我想在Rails中使用它。我怎么能在ActiveRecord中做到这一点?@asiniy:我不知道。我不使用Rails,我避免使用类似瘟疫的ORMs,正是为了解决这些问题。但使用函数应该比使用复杂的表达式容易得多。这似乎是正确的,但我忘记了所有字符串都可以只有[a-z]值。因此,类型numeric:的输入语法无效。请参阅更新的问题