1

我在 BigQuery 中有一个创建日期和上次修改日期的任务。如果可能,我希望能够按日期在同一张表中报告任务打开和任务关闭事件的数量。

view: tasks {
  derived_table: {
    sql:
        SELECT *
        FROM UNNEST(ARRAY<STRUCT<CREATED_DATE DATE, LAST_MODIFIED DATE, ID INT64, STATE STRING>>[
        ('2020-12-01', '2020-12-01', 1, "OPEN"),
        ('2020-12-01', '2020-12-03', 2, "CLOSED"),
        ('2020-12-02', '2020-12-03', 3, "CLOSED"),
        ('2020-12-03', '2020-12-05', 4, "OPEN"),
        ('2020-12-05', '2020-12-05', 5, "CLOSED")])
      ;;
  }
  dimension_group: created {
    type: time
    datatype: date
    sql: ${TABLE}.created_date ;;
  }
  dimension_group: last_modified {
    type: time
    datatype: date
    sql: ${TABLE}.last_modified ;;
  }
  dimension: id {
    type: number
  }
  dimension: state {
    type: string
  }
  measure: number_of_tasks {
    type: count_distinct
    sql: ${id} ;;
  }
  measure: number_of_open_tasks {
    type: count_distinct
    sql: ${id} ;;
    filters: {
      field: "state"
      value: "OPEN"
    }
  }
  measure: number_of_closed_tasks {
    type: count_distinct
    sql: ${id} ;;
    filters: {
      field: "state"
      value: "CLOSED"
    }
  }
}
explore: tasks {}

我可以使用创建日期获取打开任务的数量。

在此处输入图像描述

我可以通过计算任务来获得关闭的任务数量,其中最后修改日期在聚合期间并且状态为关闭,并带有过滤度量。

在此处输入图像描述

但是,如果我尝试将这些组合在一个表中,我会为每个日期组合得到一行。

在此处输入图像描述

如何按日期计算任务状态更改?

日期 打开的任务数 已关闭任务数
2020-12-01 2 0
2020-12-02 1 0
2020-12-03 1 2
2020-12-04 0 0
2020-12-05 1 1
4

1 回答 1

0

一位同事提出了解决方案。将任务表堆叠在其自身上会为每个任务创建(最多)两行。

view: tasks {
  derived_table: {
    sql:
        WITH tab AS (
          SELECT *
          FROM UNNEST(ARRAY<STRUCT<CREATED_DATE DATE, LAST_MODIFIED DATE, ID INT64, STATE STRING>>[
          ('2020-12-01', '2020-12-01', 1, "OPEN"),
          ('2020-12-01', '2020-12-03', 2, "CLOSED"),
          ('2020-12-02', '2020-12-03', 3, "CLOSED"),
          ('2020-12-03', '2020-12-05', 4, "OPEN"),
          ('2020-12-05', '2020-12-05', 5, "CLOSED")])
        )
        SELECT *, 1 open_count, 0 closed_count, created_date AS action_date 
        FROM tab
        UNION DISTINCT
        SELECT *, 0 open_count, 1 closed_count, last_modified AS action_date 
        FROM tab
        WHERE state = "CLOSED"
      ;;
  }
  dimension_group: created {
    type: time
    datatype: date
    sql: ${TABLE}.created_date ;;
  }
  dimension_group: last_modified {
    type: time
    datatype: date
    sql: ${TABLE}.last_modified ;;
  }
  dimension_group: action {
    type: time
    datatype: date
    sql: ${TABLE}.action_date ;;
  }
  dimension: id {
    type: number
  }
  dimension: state {
    type: string
  }
  dimension: open_count {
    type: number
    hidden: yes
  }
  dimension: closed_count {
    type: number
    hidden: yes
  }
  measure: number_opened{
    type: sum
    sql: ${open_count} ;;
  }
  measure: number_closed {
    type: sum
    sql: ${closed_count} ;;
  }
}
explore: tasks {}

然后可以计算打开和关闭的标签。

打开和关闭标志的计数

于 2021-01-19T09:16:17.880 回答