私はPostgreSQLのテーブルを持っており、ケースを階層的にそれを注文したいを考えながら。次に例を示します。PostgreSQLの発注テーブル階層条件
> Name | Port
> ----------------------------
> Switch1 | GigabitEthernet2/2
> Switch1 | GigabitEthernet1/2
> Switch2 | 1.23
> Switch2 | 1.21
> Switch1 | GigabitEthernet2/1
> Switch1 | GigabitEthernet1/1/3
> Switch1 | FastEthernet1/1/14
> Switch1 | FastEthernet3/0/19
> Switch1 | FastEthernet3/0/20
> Switch2 | Port-Channel3
> Switch1 | GigabitEthernet3/0/4
> Switch1 | GigabitEthernet3/1/3
> Switch1 | FastEthernet3/0/2
> Switch2 | 1.14
> Switch2 | Port-Channel6
それはそのようにする必要があり、ソート/ご注文後:
> Name | Port
> ----------------------------
> Switch1 | FastEthernet1/1/14
> Switch1 | FastEthernet3/0/2
> Switch1 | FastEthernet3/0/19
> Switch1 | FastEthernet3/0/20
> Switch1 | GigabitEthernet1/2
> Switch1 | GigabitEthernet2/1
> Switch1 | GigabitEthernet2/2
> Switch1 | GigabitEthernet1/1/3
> Switch1 | GigabitEthernet3/0/4
> Switch1 | GigabitEthernet3/1/3
> Switch2 | 1.14
> Switch2 | 1.21
> Switch2 | 1.23
> Switch2 | Port-Channel3
> Switch2 | Port-Channel6
私はCASE BY ORDERで何かをしようとしています。ここに例があります:
SELECT device.name, device_port.port
FROM device
JOIN device_port ON device.ip = device_port.ip
ORDER BY CASE WHEN device_port.port like 'Fast%' THEN string_to_array(substr(device_port.port, position('/' in device_port.port)-1),'/')::float[]
WHEN device_port.port like 'Gigabit%' THEN string_to_array(substr(device_port.port,16),'/')::float[]
WHEN device_port.port like 'Port-channel%' THEN string_to_array(substr(device_port.port,13),'/')::float[] END;
しかし、この方法では、最初にdevice.nameとポートの名前で注文する可能性はありません。
誰もこれを解決する方法のアイデアを...
> Name | Port
> ----------------------------
> Switch1 | FastEthernet1/1/14
> Switch1 | GigabitEthernet1/2
> Switch1 | FastEthernet3/0/2
> Switch1 | GigabitEthernet3/0/4
などなど:結果は次のように例えばでしょうか?結果なしで何時間も試しました。
ありがとうございます!
編集された答えをご覧ください。 'regexp_replace(port、 '[0-9/\。]'、 ''、 'g')の空文字列は、' '[0-9/\。]' 'にマッチするすべてのトークンが ' 「ポート」。 – klin
これは簡単な解決策のようです。今はすべてが期待通りに機能します。私はあなたの支援に感謝します! – Grunner