如何以非平凡的顺序订购SQL输出?

时间:2019-07-17 01:35:14

标签: sql

我试图将我的SQL输出排序为特定顺序,但是有很多选项,所以我不能只输入它们。

我有一个表,该表的列(binlocation)具有3或4个字符的字符串(取决于以下内容)。所有字符串都具有以下格式的“字母数字字母”,其范围为{A-G} {1-30} {A-I},因此如下所示:A1A,A2A,...,G30I

我有一种非常特殊的方式希望订购它们,但是有1890个选项,因此我不能仅通过数组或类似命令订购。

我希望它按第一个值排序,然后对于每个第一个值,我希望按第二个值排序,但对于第二个值,则按第三个值排序。例如: A1A,A1B,A1C,...,A2A,A2B,A2C,...,B1A,B1B,B1C,...等

要增加另一级别的复杂性,我希望以'A','B','C'和'D'结尾的所有内容首先显示。因此,例如: A1A,A1B,A1C,A1D,A2A,A2B,A2C,A2D,...,G1A,G1B,G1C,G1D,G2A,G2B,G2C,G2D,... A1E,A1F,A1G,A1H,A1I,A2E ,A2F等...

到目前为止,我所做的是将字符串分成3列,然后相应地对3列进行排序:

select binlocation as "Bin", left(binlocation,1) as isle, cast(left(right(binlocation,length(binlocation)-1), length(binlocation)-2) as int) as row, right(binlocation,1) as height
from icprod
where discontinued = true
and quantityavailable >= 1
order by isle, row, height

这使我处于一阶的binlocation。但是,我不知道如何只让它先按A,B,C,D排序高度。我希望这就像两个输出的并集一样简单:

select binlocation as "Bin", left(binlocation,1) as isle, cast(left(right(binlocation,length(binlocation)-1), length(binlocation)-2) as int) as row, right(binlocation,1) as height
from icprod
where discontinued = true
and quantityavailable >= 1
and right(binlocation,1) in ('A', 'B', 'C', 'D')
order by isle, row, height

UNION

select binlocation as "Bin", left(binlocation,1) as isle, cast(left(right(binlocation,length(binlocation)-1), length(binlocation)-2) as int) as row, right(binlocation,1) as height
from icprod
where discontinued = true
and quantityavailable >= 1
and right(binlocation,1) in ('E', 'F', 'G', 'H', 'I')
order by isle, row, height

抛出了很多错误,说我的列是意外令牌。

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我认为这可以满足您的要求

payload = {'username': 'name@domain.com', 'password': 'thisisit'}
with requests.Session() as s:
    s.post('https://example.com/login', data=payload)
    url = 'https://example.com/inquiry_detail?action=download_inquiry_items&inquiry_id=1'
    r = requests.get(url, allow_redirects = True)
    with open('foobar', 'wb') as f:
        f.write(r.content)

逻辑是:

  • 先放入A / B / C / D的
  • 然后按首字母顺序
  • 在3位数字值之前按4位数字顺序排序
  • 按整个值(即按数字)排序