我有一个列有全部整数的列。在INT
列上生成密钥时,我需要按下0和负数(有序DESC)并将正键保持在顶部(有序ASC / DESC)。我提出了两种方法,但我不确定哪种方法最好
方法1:更整洁但不确定可扩展性
WITH t1(c1) AS
(
SELECT 11 UNION ALL
SELECT 2 UNION ALL
SELECT 0 UNION ALL
SELECT -1 UNION ALL
SELECT -11
)
SELECT c1, ROW_NUMBER() OVER (ORDER BY
CASE WHEN c1 <=0 THEN c1 END DESC ,
CASE WHEN c1 >0 THEN c1 END ASC) keys
FROM t1 ;
方法2:可扩展
WITH t1(c1) AS
(
SELECT 11 UNION ALL
SELECT 2 UNION ALL
SELECT 0 UNION ALL
SELECT -1 UNION ALL
SELECT -11
)
SELECT c1, ROW_NUMBER() OVER (ORDER BY c1 ASC) keys
FROM t1
WHERE c1 >0
UNION ALL
-- Need to include Max keys from previous step in actual implementation
SELECT c1,
maxkeys + ROW_NUMBER() OVER (ORDER BY c1 DESC) keys
FROM t1
WHERE c1 <= 0
请为可扩展性(BIGINT
范围)建议任何其他更好的替代方案。
答案 0 :(得分:0)
经过一轮性能测试后,我们发现选项1在这种情况下更优雅,更优化。此外,此代码段是为动态生成的众多表格生成的,它更易于配置和打开和关闭。