简体   繁体   English

在红移中进行不同汇总的更好方法?

[英]Better way to do distinct rollup in redshift?

What is the best way to write a query that does a rollup of distinct counts over various discrete time ranges on redshift?编写查询的最佳方法是在 redshift 上的各种离散时间范围内汇总不同的计数?

For example, if you have some phone numbers collected for leads in various organizations and want to find out how many distinct phone numbers were created weekly, monthly, quarterly, etc... what is the best way to do it?例如,如果您为各个组织的潜在客户收集了一些电话号码,并且想了解每周、每月、每季度等创建了多少不同的电话号码……最好的方法是什么?

This is the best I could come up with:这是我能想到的最好的:

SELECT
  organization,
  sum(weekly) as weekly,
  sum(monthly) as monthly,
  sum(quarterly) as quarterly,
  sum(yearly) as yearly
FROM (
    SELECT 
      organization,
      COUNT(DISTINCT phoneNumber) as weekly,
      null as monthly,
      null as quarterly,
      null as yearly
    FROM Lead
    WHERE createdAt >= current_date - interval '7 days'
    GROUP BY organization

      UNION ALL

    SELECT
      organization,
      null as weekly,
      COUNT(DISTINCT phoneNumber) as monthly,
      null as quarterly,
      null as yearly
    FROM Lead
    WHERE createdAt >= current_date - interval '1 month'
    GROUP BY organization

        UNION ALL

    SELECT
      organization,
      null as weekly,
      null as monthly,
      COUNT(DISTINCT phoneNumber) as quarterly,
      null as yearly
    FROM Lead
    WHERE createdAt >= current_date - interval '3 months'
    GROUP BY organization

        UNION ALL

    SELECT
      organization,
      null as weekly,
      null as monthly,
      null as quarterly,
      COUNT(DISTINCT phoneNumber) as yearly
    FROM Lead
    WHERE createdAt >= current_date - interval '1 year'
    GROUP BY organization
) GROUP BY organization

Any way to make the query faster / easier to understand?有什么方法可以使查询更快/更容易理解?

If I understand correctly, you would just use conditional aggregation:如果我理解正确,您只需使用条件聚合:

SELECT organization,
       COUNT(DISTINCT CASE WHEN created_at >= current_date - interval '7 day' THEN phoneNumber END) as weekly,
       COUNT(DISTINCT CASE WHEN created_at >= current_date - interval '1 month' THEN phoneNumber END) as monthly,
       COUNT(DISTINCT CASE WHEN created_at >= current_date - interval '3 month' THEN phoneNumber END) as quarterly,
       COUNT(DISTINCT CASE WHEN created_at >= current_date - interval '1 year' THEN phoneNumber END) as yearly
FROM Lead
WHERE createdAt >= current_date - interval '1 year'
GROUP BY organization;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM