字段中的值以正数,负数,字母的顺序排列。正数从小到大。负数从大到小的顺序排列,字母以长短排列

数据库是pgsql

1.png

呈现的是这种形式的效果2.png

FineReport Latty 发布于 2022-11-16 09:24 (编辑于 2022-11-16 10:31)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
CD20160914Lv8专家互助
发布于2022-11-16 09:39(编辑于 2022-11-16 11:25)

现在没有这个环境了。测试不了。你自己用正则提取出来后。。。再对辅助的一列排序就行了

pgsql  有很强大的正则匹配函数。你自己分别提取出来后。再排序了。。

模式匹配 (postgres.cn)

pgsql提取数字。正则负数有小数的类似这样

select regexp_replace('提取123.11abc提取','[^\d.\d]','','g')

postgresql正则表达使用_二两芝麻的博客-CSDN博客

select 

t3.数据

 from (

select 

t2.*,

case when t2.判断=3 then t2.内容 else 9999 end as 文本,

case when t2.判断=1 then t2.内容 else 9999 end as 正数,

case when t2.判断=2 then t2.内容 else 9999 end as 负数

 from (

select 

t.数据,

case when length(regexp_replace(t.数据,'[^a-zA-Z]','','g')) >0 then length(t.数据) 

when length(regexp_replace(t.数据,'[^(\-|\+)?\d+(\.\d+)?]','','g'))>0 then cast (regexp_replace(t.数据,'[^(\-|\+)?\d+(\.\d+)?]','','g') as integer)

else 999999 end as  内容,

case when length(regexp_replace(t.数据,'[^a-zA-Z]','','g'))>0 then 3/*如果有字母就先赋值一个3.为了排序用*/

when length(regexp_replace(t.数据,'[^(\-|\+)?\d+(\.\d+)?]','','g'))>0 and cast (regexp_replace(t.数据,'[^(\-|\+)?\d+(\.\d+)?]','','g') as integer)>0 then

 1 else 2 end as 判断

from (

select '-123' as 数据

union all

select '78' as 数据

union all

select '78abc_16.45_AET' as 数据

union all

select '-950' as 数据

union all

select '66' as 数据

union all

select '78abc' as 数据

) t

) t2

) t3

order by t3.判断,t3.正数 asc, t3.负数 asc,t3.文本

结果:

image.png

  • Latty Latty(提问者) 好像只能提取数字,
    2022-11-16 10:10 
  • CD20160914 CD20160914 回复 Latty(提问者) 我只是随便写的。你要提取字母与文本都是可以的呀。我没有写而已。我上面第二个连接里面就有提取字母的正则表达试。。你配合一起用就行了。。像你数字与字母在一起的。你是想如何??
    2022-11-16 10:13 
  • Latty Latty(提问者) 回复 CD20160914 第一步 正数排列从小到大 第二步 负数排列从大到小 第三步 字母排列从短到长
    2022-11-16 10:18 
  • CD20160914 CD20160914 回复 Latty(提问者) 你先不要说你的第几步。。我问你的是数字与字母在一起的时候想如何????s2这种情况要如何处理
    2022-11-16 10:20 
  • CD20160914 CD20160914 回复 Latty(提问者) 没有汉字吧?只有字母,数字。正数。负数。和小数这样的?
    2022-11-16 10:31 
  • 2关注人数
  • 375浏览人数
  • 最后回答于:2022-11-16 11:25
    请选择关闭问题的原因
    确定 取消
    返回顶部