创建自定义排序 sql,重复应用于查询

Create custom sort sql, apply repeatedly to queries

我有一个 table 记录 96 孔网格中的样本位置(位置命名为 A01-H12)。我想让我的查询按特定顺序导出数据,按列(数字)而不是按行字母。

示例 plateTable:

PlateID SampleID WellPosition
plate1 Sample4 B02
plate1 Sample10 A01
plate1 Sample1 B01
plate1 Sample30 A02
plate1 Sample5 C01
plate1 Sample8 F01
plate1 Sample32 B08
plate1 Sample9 A12

我的典型查询是

Select * 来自 plateTable 其中 PlateID="plate1"

我希望我的输出按所有“01”排序,然后是所有“02”...,每个数字内按字母排序。

期望的输出:

PlateID SampleID WellPosition
plate1 Sample10 A01
plate1 Sample1 B01
plate1 Sample5 C01
plate1 Sample8 F01
plate1 Sample30 A02
plate1 Sample4 B02
plate1 Sample32 B08
plate1 Sample9 A12

嗯位置总是1个字母,后跟2个数字。有没有办法指定先取出最后 2 位数字排序,然后按第一个字母排序?或者其他设置排序顺序的方法?

使用 RIGHT() 函数提取 WellPosition 的最后 2 个字符,并首先按这些字符排序,然后按 WellPosition:

SELECT * 
FROM plateTable 
WHERE PlateID = 'plate1'
ORDER BY RIGHT(WellPosition, 2), WellPosition

参见demo
结果:

PlateID SampleID WellPosition
plate1 Sample10 A01
plate1 Sample1 B01
plate1 Sample5 C01
plate1 Sample8 F01
plate1 Sample30 A02
plate1 Sample4 B02
plate1 Sample32 B08
plate1 Sample9 A12