[英]order a table in postgresql based in a specific string field with format number/year
我有一个内部通信表,其中有一个名为 ci_num 的字段,它的格式是数字/年份。 例如:23/2020。
我想在这个领域订购我的桌子。 我已经搜索但没有找到如何创建 function 以这种方式订购。
在 java 中,我使用自定义排序 function 执行此操作。 我试图在 postgres 中做同样的事情,但没有成功。
使用升序:
select * from cis c order by ci_num asc
结果:
ci_id|ci_num |tipo_cod|status_cod|setor_cod|usuario_id -----|-------|--------|----------|---------|---------- 10|10/2020|GE |EC |NUTEC | 1 11|11/2020|CO |DB |NUCAD | 4 1|1/2020 |CO |DE |NUCAD | 1 12|12/2020|CO |NG |NUTEC | 4 13|13/2020|CO |AT |NUOPE | 4 14|14/2020|CO |NG |NUTEC | 4 15|15/2020|GE |DE |NUOPE | 4 16|16/2020|CO |NG |NUTEC | 4 17|17/2020|CO |CA |NUTEC | 4
使用降序变得更糟:
select * from cis c order by ci_num desc
ci_id|ci_num |tipo_cod|status_cod|setor_cod|usuario_id -----|-------|--------|----------|---------|---------- 9|9/2020 |IN |AT |NUOPE | 4 8|8/2020 |CO |NG |NUTEC | 4 7|7/2020 |GE |CA |NUTEC | 4 6|6/2020 |HE |CR |NUCAD | 1 54|54/2020|GE |EA |NUTEC | 4 53|53/2020|GE |EA |NUOPE | 1 52|52/2020|GE |EA |NUOPE | 1
谢谢你的帮助!
您可以只使用字符串函数:
order by
substring(ci_num from '\d+$'),
substring(ci_num from '^\d+')::int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.