-- 从 air_conditioner_crash_log 表中获取指定 id 损坏的次数以及损坏次数的排名
SELECT crash_count, crash_rank
FROM (
    SELECT id,
           COUNT(*) AS crash_count,
           RANK() OVER (ORDER BY COUNT(*) DESC) AS crash_rank
    FROM air_conditioner_crash_log
    GROUP BY id
) AS ranked_data
WHERE id = ?
-- 如果该 id 没有损坏记录，则返回 0 次损坏和对应的最后一名
UNION
SELECT 0 AS crash_count,
    (SELECT COUNT(DISTINCT id) + 1 FROM air_conditioner_crash_log) AS crash_rank
FROM (
    SELECT DISTINCT id
    FROM air_conditioner_crash_log
) AS ranked_data
WHERE NOT EXISTS (
    SELECT 1
    FROM air_conditioner_crash_log
    WHERE id = ?
);