PostgresSQL查询离指定时间戳最近的数据 - xd21303/Notebook GitHub Wiki

RECORD_TIME是时间列,'2020-09-01 15:40:00'是指定的时间

SELECT *,
	CASE
	    WHEN I < INTERVAL '0' THEN - I
	    ELSE I
	END AS ABS_I
FROM
	(SELECT *,
		(RECORD_TIME - '2020-09-01 15:40:00') AS I
		FROM H_HEALTH_CHECK_DEDUCT
		WHERE HEALTH_CHECK_ID = '410') AS FOO
ORDER BY ABS_I
LIMIT 1;