当我发现时,我有点希望它可以在声明中NULLS LAST推广到“X LAST”CASEORDER BY,我有点希望它可以在查询部分的
看来并非如此。
我正在尝试按两列对表进行排序(简单),但以特定顺序获取输出(简单),其中一列的一个特定值出现在最后(完成了......丑陋)。
假设这些列是zone和status(不要怪我命名列zone- 我没有命名它们)。status仅需要 2 个值(“U”和“S”),而zone可以采用大约 100 个值中的任何一个。
zone的值的一个子集是(在伪正则表达式中) IN[0-7]Z,并且它们位于结果中的第一个。这很容易用一个CASE.
zone还可以采用值“Future”,该值应出现在结果的最后。
按照我典型的 kludgy-munge 方式,我简单地将CASE值强加为 1000,如下所示:
group by zone, status
order by (
case when zone='IN1Z' then 1
when zone='IN2Z' then 2
when zone='IN3Z' then 3
.
. -- other IN[X]Z etc
.
when zone = 'Future' then 1000
else 11 -- [number …Run Code Online (Sandbox Code Playgroud) 我有一个NewSchemaSafe.sql基于项目目录创建新模式的脚本;它是从 Windows 命令行调用的,如下所示:
for %%a in (.) do set this=%%~na
-- other stuff here
psql -U postgres -d SLSM -e -v v1=%this% -f "NewSchemaSafe.sql"
Run Code Online (Sandbox Code Playgroud)
NewSchemaSafe.sql 如下:
-- NewSchemaSafe.sql
-- NEW SCHEMA SETUP
-- - checks if schema exists
-- - if yes, renames existing with current monthyear as suffix
-- NOTE: will always delete any schema with the 'rename' name (save_schema)
-- since any schema thus named must have resulted from this script
-- on this date - …Run Code Online (Sandbox Code Playgroud)