[英]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.