文档

Quick BI如何实现去重后的累计计算

更新时间:
一键部署

产品名称

Quick BI

产品模块

数据集

概述

本文向您介绍参数SQL的写法,帮助您实现部分累计计算的应用场景。

问题描述

现有订单明细表,记录了每天的购买商品用户,如何展示每天的购买用户数,及截止到每天的累计购买用户数

解决方案

Quick BI仪表板中自带累计计算的统计方式,不过该配置项有部分限制,比如:只有固定的时间段内累计(年累计、月累计、季累计等)、对字段的聚合方式有要求(只有求和、计数、最大值、最小值可以使用)。

该案例中,用户数是需要去重计数的,无法通过上述配置的方式求得累计的去重计数的值,需要通过SQL取出该字段,可参考如下SQL

select a.report_date,
       count(distinct a.customer_name) as cust_num,
       count(distinct b.customer_name) as all_cust_num
from(
    select report_date,customer_name
    from company_order_sales_record
    where ${report_date:report_date}
    group by report_date,customer_name
)a
left join(
    select report_date,customer_name
    from company_order_sales_record
    where ${report_date:report_date}
    group by report_date,customer_name
)b on a.report_date>=b.report_date
group by a.report_date
该SQL举例中,加了日期参数,方便您统计任意筛选时间段内的累计计数的结果。如果您只是需要查看历史累计当日的用户数,不需要加参数条件;如果您还有其他条件(比如各店铺每日的累计用户数),那么就需要再加参数。您可以根据实际应用场景,修改SQL写法。

更多信息

如何写参数SQL:https://help.aliyun.com/knowledge_detail/273069.html

相关文档

累计计算:https://help.aliyun.com/document_detail/211447.html

  • 本页导读
文档反馈