I have a table like this:
Category Version
B 1.8.11
B 1.20.138
B 1.29.01
B 1.30.89
B 1.3.114
Now, I want to get the latest version. By default, order by doesn't work because 1.8.11 is treated the highest.
I understand one way is to use split_part and substring and get the latest version which in this case will be 1.30.89.
Just wanted to understand if there is a better way to achieve this.
Edit1:
I am using redshift postgresql.So, not all postgresql functions are available.
Another way I figured out yet is something like this. Herein I pad zeroes to the left and then I have to remove them before reporting the actual version.
(TO_CHAR(SPLIT_PART(version,'.',1),'fm000') + '.' + TO_CHAR(SPLIT_PART(version,'.',2),'fm000') + '.' + TO_CHAR((case when SPLIT_PART(version,'.',3) = '' then '0' else SPLIT_PART(version,'.',3) end),'fm000')) AS padded_version
EDIT2: I got a slightly better method. So, just sharing it here:
select category, version from
(SELECT CATEGORY,
VERSION,
ROW_NUMBER() OVER (PARTITION BY CATEGORY ORDER BY SPLIT_PART(VERSION,'.',1)::INT DESC,SPLIT_PART(VERSION,'.',2)::INT DESC),SPLIT_PART(VERSION,'.',3)::INT DESC)
from table1)
where row_number = 1
Thanks!