同一数据的多个聚合

Multiple aggregations for the same data

我有两个问题:

SELECT 
    COUNT(warehouseCode) as count,
    warehouseCode
  FROM 'sparePartEventsTable'
  WHERE 
    sparePartCode = 'SP.0000' AND 
    sparePartConsumed = 'true' 
  GROUP BY warehouseCode
  ORDER BY count DESC

  SELECT 
    COUNT(*) as eventsCount,
    DATE(TIMESTAMP_SECONDS(epochSeconds)) as day
  FROM 'sparePartEventsTable'
  WHERE 
    sparePartCode = 'SP.0000' AND 
    sparePartConsumed = 'true' 
  GROUP BY day
  ORDER BY day  

如您所见,基础数据是相同的,但我返回了两个不同的聚合。 SQL 有没有办法避免两次击中磁盘?

BigQuery 或 Postgres 如何实现?

在 mongodb 中,我会在底层公共数据上构建一个游标,然后编写一个聚合管道,输出两个结果。

编辑:UNION ALL 似乎是第一个解决方案,但至少在 postgres 中它会扫描两次。谢尔盖在评论中建议 GROUPING SETS,但不幸的是,它们在 BigQuery 中不可用。将接受使用 Postgres 或 BigQuery 方言的答案,如果同时发布两种解决方案则加分 :)

考虑以下 (BigQuery) 选项

with temp as (
  select 
    warehouseCode, 
    count(*) over(partition by warehouseCode) as `count`,
    date(timestamp_seconds(epochSeconds)) as day,
    count(*) over(partition by unix_date(date(timestamp_seconds(epochSeconds)))) as eventsCount
  from `sparePartEventsTable`
  where sparePartCode = 'SP.0000' 
  and sparePartConsumed = 'true' 
)
select distinct 'warehouseCode' type, '' || warehouseCode as value, `count` from temp union all
select distinct 'day', string(day), eventsCount from temp

输出如下