如何在 Looker 中按日期计算状态变化?
How to count state changes by date in Looker?
我在 BigQuery 中有一个任务,其中包含创建日期和上次修改日期。如果可能的话,我希望能够在同一个 table 中按日期报告任务打开和任务关闭事件的数量。
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 {}
我可以使用创建日期获取打开的任务数。
我可以通过对任务进行计数来获取已关闭的任务数,其中最后修改日期在聚合期间并且状态为已关闭,并具有过滤措施。
但是,如果我尝试将这些组合成一个 table,我会为每个日期组合得到一行。
如何按日期统计任务状态变化?
Date
Number of Opened Tasks
Number of Closed 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
一位同事提出了一个解决方案。堆叠任务 table 本身会为每个任务创建(最多)两行。
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 {}
然后可以计算打开和关闭的标签。
我在 BigQuery 中有一个任务,其中包含创建日期和上次修改日期。如果可能的话,我希望能够在同一个 table 中按日期报告任务打开和任务关闭事件的数量。
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 {}
我可以使用创建日期获取打开的任务数。
我可以通过对任务进行计数来获取已关闭的任务数,其中最后修改日期在聚合期间并且状态为已关闭,并具有过滤措施。
但是,如果我尝试将这些组合成一个 table,我会为每个日期组合得到一行。
如何按日期统计任务状态变化?
Date | Number of Opened Tasks | Number of Closed 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 |
一位同事提出了一个解决方案。堆叠任务 table 本身会为每个任务创建(最多)两行。
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 {}
然后可以计算打开和关闭的标签。