How do you make a top-three report reproducible across ties?

ORDER BY applies keys in sequence and LIMIT takes the requested prefix. A unique final key closes ties.

SQLite example and interpretation

Three people score eight but only two slots remain. Ordering their IDs defines a tie policy. A hard row limit is unsuitable when all peers must be shown.

WITH r(id,s) AS (VALUES(3,8),(1,8),(2,8),(4,9)) SELECT id,s FROM r ORDER BY s DESC,id LIMIT 3;

Result [[4, 9], [1, 8], [2, 8]]

中文

报告只展示前三名,平分时如何稳定选人?

ORDER BY 逐个应用排序键,LIMIT 截取排序结果。追加唯一编号可为同分记录建立全序。

SQLite 示例与适用条件

三人都得 8 分却只剩两个位置,用编号升序作公开的同分规则。若业务要求同名次全部展示,固定行数会截掉并列者。

WITH r(id,s) AS (VALUES(3,8),(1,8),(2,8),(4,9)) SELECT id,s FROM r ORDER BY s DESC,id LIMIT 3;

结果 [[4, 9], [1, 8], [2, 8]]

References / 参考来源

Keep reading / 继续阅读