Результаты подзапроса - ARRAY. Если результатов нет, длина будет равна 0 (не MISSING). В результатах подзапроса есть EXIST / NOT EXIST. Поскольку вам тоже нужны результаты и не нужно повторять запрос дважды. Вы также можете использовать следующий подход.
CB 6.5 или выше
WITH result AS (SELECT DATE_FORMAT_STR(crdate, "2020-01-01") AS date,
DATE_PART_STR(crdate, "hour") AS hour,
COUNT (1) AS count
FROM data_KH AS ev
LET crdate = DATE_ADD_STR(SPLIT(ev.auditDetail.createTime, ".")[0], 7, "hour")
WHERE type_ = "user"
AND crdate > CLOCK_TZ ("Asia/Bangkok", "1111-11-11")
AND SPLIT (loginId,"@")[1] NOT IN ["yopmail.com", "ymail.com", "accoliteindia.com"]
GROUP BY DATE_FORMAT_STR(crdate, "2020-01-01"), DATE_PART_STR(crdate, "hour"))
SELECT d.*
FROM (CASE WHEN ARRAY_LENGTH(result) > 0 THEN result ELSE [{"date": CLOCK_TZ("Asia/Bangkok","2020-01-01"),
"hour":DATE_PART_STR(CLOCK_TZ("Asia/Bangkok"), "hour"),
"count":0}]
END) AS d
ORDER BY d.date, d.hour;
Pre CB 6.50
SELECT d.*
FROM ARRAY_FLATTEN ((SELECT RAW CASE WHEN ARRAY_LENGTH(result) > 0
THEN result
ELSE [{"date": CLOCK_TZ("Asia/Bangkok","2020-01-01"),
"hour":DATE_PART_STR(CLOCK_TZ("Asia/Bangkok"), "hour"),
"count":0}]
END
LET result = (SELECT DATE_FORMAT_STR(crdate, "2020-01-01") AS date,
DATE_PART_STR(crdate, "hour") AS hour,
COUNT (1) AS count
FROM data_KH AS ev
LET crdate = DATE_ADD_STR(SPLIT(ev.auditDetail.createTime, ".")[0], 7, "hour")
WHERE type_ = "user"
AND crdate > CLOCK_TZ ("Asia/Bangkok", "1111-11-11")
AND SPLIT (loginId,"@")[1] NOT IN ["yopmail.com", "ymail.com", "accoliteindia.com"]
GROUP BY DATE_FORMAT_STR(crdate, "2020-01-01"), DATE_PART_STR(crdate, "hour"))),1) AS d
ORDER BY d.date, d.hour;
Вы также можете использовать STR_TO_TZ (ev.auditDetail.createTime , "Азия / Бангкок") вместо DATE_ADD_STR (SPLIT (ev.auditDetail.createTime, ".") [0], 7, "час") https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/datefun.html