SELECT AREA,ATIMEPOINT,BTIMEPOINT,
--TIMESTAMPDIFF(8,CHAR(ATIMEPOINT-BTIMEPOINT)),
HOUR(TIME(ATIMEPOINT)-TIME(BTIMEPOINT))
FROM (
SELECT TIMEPOINT ATIMEPOINT,AREA, (
SELECT TIMEPOINT
FROM TENV.AIR_CITYHOURAQI_PUBLISH B WHERE AQI >= 500
AND TO_CHAR(TIMEPOINT,'YYYY-MM') >= '2017-03' AND TO_CHAR(TIMEPOINT,'YYYY-MM') <= '2017-05'
AND B.CITYCODE = A.CITYCODE
AND ROWNUM = 1
) BTIMEPOINT
FROM TENV.AIR_CITYHOURAQI_PUBLISH A
WHERE AQI >= 500
AND TO_CHAR(TIMEPOINT,'YYYY-MM') >= '2017-03' AND TO_CHAR(TIMEPOINT,'YYYY-MM') <= '2017-05'
)