我有一列包含整个整数范围。在列上生成键的同时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
范围)替代方案。