Clickhouse 给了我们很大的鼓励。然而,当我们尝试将所有现有脚本移植到 Clickhouse 时,我们遇到了一些障碍。 例如:
CUMULATIVE SUM or RUNNING TOTAL
。我们正在尝试找到窗口函数的等效项,例如SUM(SALES) OVER (PARTITION BY PRODUCT ORDER BY SALES)
"Percent to total"
的脚本。
例如
Product | Sales
P1 100
P2 200
P3 150
P4 50
我们正在考虑编写一个可以填充的脚本
Product, Sales, PercenttoTotal
P1, 100, 20%
P2, 200, 40%
P3, 150, 30%
P4, 50, 10%
是否有一种通过 Clickhouse 中的各种功能快速完成此任务的方法。预先感谢。
占总数的百分比:
SELECT
productid,
count() as per_product_count,
per_product_count * 100 / ( SELECT count() from prod_sales ) as percent_to_total
FROM prod_sales
GROUP BY productid
累计总和或运行总计:
最新版本(v1.1.54310-stable)添加了对函数 runningIncome (尚未记录)的支持,该函数应该像 runningDifference 一样工作,但应该计算总和而不是差值。
也可以在 github 上查看我对此问题的回答:
使用
SUM
+ OVER
。例如:
SUM(events_count) OVER (ORDER BY interval)
它对我有帮助。在这里找到解决方案:https://www.interviewquery.com/p/sql-cumulative-sum-guide