※追記 変換ツールを作りました。 https://yuyabu.github.io/OrderByWithCaseExpressionGenerator/ SQLのORDER BYでは昇順・降順で特定のカラム(単独or 組み合わせ)の並びを指定できるが、CASE式を組み合わせて、任意のソート順を指定出来ることを最近知った。 (SQL内だけでこういうことが実現できるのが衝撃的) WITH students as( SELECT 1 AS sid, '高橋太郎' AS sname FROM DUAL UNION ALL SELECT 2 AS sid, '山田幸助' AS sname FROM DUAL UNION ALL SELECT 3 AS sid, '鈴木美恵子' AS sname FROM DUAL UNION ALL SELECT 4 AS sid, '織田信長' AS sname
![SQLのORDER BY で任意のソート順を設定する - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/bd58def245e3b81cb38e965f9cc3672f0a8e8dc9/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-412672c5f0600ab9a64263b751f1bc81.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTk3MiZoPTM3OCZ0eHQ9U1FMJUUzJTgxJUFFT1JERVIlMjBCWSUyMCVFMyU4MSVBNyVFNCVCQiVCQiVFNiU4NCU4RiVFMyU4MSVBRSVFMyU4MiVCRCVFMyU4MyVCQyVFMyU4MyU4OCVFOSVBMCU4NiVFMyU4MiU5MiVFOCVBOCVBRCVFNSVBRSU5QSVFMyU4MSU5OSVFMyU4MiU4QiZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZ0eHQtY29sb3I9JTIzMjEyMTIxJnR4dC1mb250PUhpcmFnaW5vJTIwU2FucyUyMFc2JnR4dC1zaXplPTU2JnM9NjM1NDI3MDhiOTU3MWQzOTgwYjUxYmJhZWVkNTBhZDE%26mark-x%3D142%26mark-y%3D57%26blend64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZoPTc2Jnc9NzcwJnR4dD0lNDBnb29kZG9vZyZ0eHQtY29sb3I9JTIzMjEyMTIxJnR4dC1mb250PUhpcmFnaW5vJTIwU2FucyUyMFc2JnR4dC1zaXplPTM2JnR4dC1hbGlnbj1sZWZ0JTJDdG9wJnM9MDZhNTRkZTVjM2M5ZTVmNzUxZDk1MWE3NTA0ODI2NGI%26blend-x%3D142%26blend-y%3D486%26blend-mode%3Dnormal%26s%3Da8d913b86201dbf70bd08278a371b1db)