Google BigQuery显示所有日期的计数,即使为零。

huangapple go评论60阅读模式
英文:

Google bigquery show all day count even if zero

问题

以下是要翻译的代码部分:

SELECT date(timestampInterval), count(*) FROM dataset.table
WHERE DATE(timestampInterval) >= "2023-03-06"
group by 1
order by 1 desc

然而,如果有日期的计数为零,结果将不包括它。 有没有办法修改这个查询以包括它。

英文:

I am using the below query to get the day wise count

SELECT date(timestampInterval), count(*) FROM `dataset.table`
WHERE DATE(timestampInterval) >= "2023-03-06"
group by 1
order by 1 desc

However if there is a date for which the count is zero, the results simply will not have it.
Is there a way to modify this query to include that as well.

答案1

得分: 3

以下是您要翻译的代码部分:

需要首先生成日期。例如:

    WITH date_range AS (
      SELECT DATE_ADD("2023-03-06", INTERVAL day DAY) AS date
      FROM UNNEST(GENERATE_ARRAY(0, DATE_DIFF(CURRENT_DATE(), DATE("2023-03-06"), DAY))) AS day
    )
    SELECT date_range.date
    FROM date_range

将为您提供从起始日期到今天的所有日期。然后使用`LEFT JOIN`将此公共表达式(CTE)与您的原始表连接,并再次计算记录的数量。
英文:

You need use first to generate the dates. For example:

WITH date_range AS (
  SELECT DATE_ADD("2023-03-06", INTERVAL day DAY) AS date
  FROM UNNEST(GENERATE_ARRAY(0, DATE_DIFF(CURRENT_DATE(), DATE("2023-03-06"), DAY))) AS day
)
SELECT date_range.date
FROM date_range

will gives you all days from the start date to today. Then join this CTE to your original table using LEFT JOIN and count the records the records again.

答案2

得分: 3

根据@gotqn的提醒,首先需要生成填充整个时间段的日期,然后将它们与现有表合并,以计算在该时间段内每一天的计数,确保没有遗漏的日期。

-- 示例数据
WITH `dataset.table` AS (
  SELECT '2023-03-06' timestampInterval UNION ALL
  SELECT '2023-03-11' timestampInterval UNION ALL
  SELECT '2023-03-13' timestampInterval
)
-- 查询开始
SELECT date, COUNT(cnt) cnt FROM (
  SELECT DATE(timestampInterval) date, 1 cnt 
    FROM `dataset.table` WHERE timestampInterval >= '2023-03-06'
   UNION ALL
  SELECT date, null FROM (
    SELECT MIN(timestampInterval) min, MAX(timestampInterval) max 
      FROM `dataset.table` WHERE timestampInterval >= '2023-03-06'
  ),UNNEST(GENERATE_DATE_ARRAY(DATE(min), DATE(max))) date
) GROUP BY 1 ORDER BY 1;

-- 查询结果
+------------+-----+
|    date    | cnt |
+------------+-----+
| 2023-03-06 |   1 |
| 2023-03-07 |   0 |
| 2023-03-08 |   0 |
| 2023-03-09 |   0 |
| 2023-03-10 |   0 |
| 2023-03-11 |   1 |
| 2023-03-12 |   0 |
| 2023-03-13 |   1 |
+------------+-----+
英文:

As @gotqn mentioned, you need to generate dates filling up the whole period first. And then you can union them with the existing table to caluate a count for each day during the period without missing day.

-- sample data
WITH `dataset.table` AS (
  SELECT '2023-03-06' timestampInterval UNION ALL
  SELECT '2023-03-11' timestampInterval UNION ALL
  SELECT '2023-03-13' timestampInterval
)
-- query starts here
SELECT date, COUNT(cnt) cnt FROM (
  SELECT DATE(timestampInterval) date, 1 cnt 
    FROM `dataset.table` WHERE timestampInterval >= '2023-03-06'
   UNION ALL
  SELECT date, null FROM (
    SELECT MIN(timestampInterval) min, MAX(timestampInterval) max 
      FROM `dataset.table` WHERE timestampInterval >= '2023-03-06'
  ),UNNEST(GENERATE_DATE_ARRAY(DATE(min), DATE(max))) date
) GROUP BY 1 ORDER BY 1;

-- query result
+------------+-----+
|    date    | cnt |
+------------+-----+
| 2023-03-06 |   1 |
| 2023-03-07 |   0 |
| 2023-03-08 |   0 |
| 2023-03-09 |   0 |
| 2023-03-10 |   0 |
| 2023-03-11 |   1 |
| 2023-03-12 |   0 |
| 2023-03-13 |   1 |
+------------+-----+

huangapple
  • 本文由 发表于 2023年4月11日 16:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定