Postgres : Select 列值的最大整数子串

Postgres : Select the maximum integer substring of a column value

我正在使用 postgresql,但在制定正确的 sql 查询时遇到问题。问题的标题可能听起来很奇怪,但这正是我想要实现的目标。我目前正在使用特定 table 执行数据管理。如果我有一个 table product 包含:

id | designation
---------------------
1  | IR15A1021
2  | IR15A1001
3  | IR15A1050
4  | AB100 
5  | AR100

我想做的是:

1) 获取恰好那些模式以开头的记录的整数子串=41=]。而by integer substring,表示起始字符串后的数字'IR15A'。 (1021,1001,1050)

2) 获取最大整数子串所以在这种情况下:

maximum integer substring : 1050 

因为所有其他 IR15A 的最大子串都小于它(1021 和 1001)。

假设条件正确。

1) IR15A 之后的子字符串始终是整数,因此无需担心类型,它始终是整数。 2) 忽略其他指定模式。只是 IR15A。

我只设法获得“%IR15A%”通配符搜索,但我还没有找到任何解决方案来从中删除整数子串并将其与其他子串进行比较以获得最大值。谢谢!

试试这个:

select max(cast(substring(designation from '....$') as int))
from tab
where designation like 'IR15A%'
  1. 使用 like 'IR15A%'
  2. 仅获取 IR15A 的行
  3. 使用 substring(designation from '....$') 删除 IR15A
  4. 使用 cast
  5. 将其转换为 int
  6. 使用max()
  7. max

Sql Fiddle 演示