繁体   English   中英

PostgreSQL - 如何提取逗号前或分隔符/逗号之间的字符串,但仅在逗号后的值大于 0

[英]PostgreSQL - How do I extract a string that is before a comma OR between a delimiter/comma, but only where the value after the comma is greater than 0

我有一列数据,如下所示:

52,0:58,1094.00
52,227.00
58,383:105,0
58,0:112,0:64,85.00
58,1376:112,0:105,0:107,0
57,2858.00
52,0:58,98
57,0:106,3317.00

我需要提取逗号“,”之前冒号“:”逗号“,”之间的每个数字,仅当逗号“,”后的值大于0时。

使用上面的数据,我想得到:

58
52
58
64
58
57
58
106

在此先感谢您的帮助!

你可以这样做:

select split_part(s.v, ',', 1)::numeric res
from mytable t
cross join lateral regexp_split_to_table(t.vals, ':') as s(v)
where split_part(s.v, ',', 2)::numeric > 0

该查询使用regexp_split_to_table()':'分隔符上的字符串拆分为新行,其中包含诸如'58,1094.00' '52,0''58,1094.00' 然后,我们使用split_part()提取两个数字,并使用第二个数字进行过滤。

DB Fiddle 上的演示

select split_part(s.v, ',', 1) res
from mytable t
cross join lateral regexp_split_to_table(t.vals, ':') as s(v)
where split_part(s.v, ',', 2)::numeric > 0
| res |
| :-- |
| 58  |
| 52  |
| 58  |
| 64  |
| 58  |
| 57  |
| 58  |
| 106 |

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM