给定一个SQL表,该表有一列(例如"dates"(,其中包含一个日期数组,哪个查询将返回数组中每个不同的日期及其发生的次数?可以向查询中添加什么子句来指定最小和最大日期范围?
给定带有日期数组的列的"myTable":
SELECT count(id), UNNEST(dates) as date
FROM myTable
GROUP BY date;
指定最小日期和最大日期,例如在2021-01-01当天或之后和2021-02-01之前,并按日期排序:
SELECT count(id), UNNEST(dates) as date
FROM myTable
WHERE EXISTS (SELECT * FROM UNNEST(dates) AS x WHERE x>='2021-01-01' AND x<'2021-02-01')
GROUP BY date
ORDER BY date ASC;