从 ClickHouse SQL 数据库分析产品可用性的完整性

Analysis of the completeness of the availability of products from the ClickHouse SQL database

文档:https://clickhouse.tech/docs/en/

目标:85% 的品牌产品可供购买

  1. 按可用性计算每个品牌的产品数量 (maxItems > 0)
  2. 按要购买的品种的可用性对品牌进行细分:
  1. 完成:
SELECT brand, COUNT(1) AS cnt
    FROM products
        WHERE maxItems > 0
    GROUP BY brand
    ORDER BY cnt DESC;

好的。

  1. 下面是一个例子来解决:

每个牌子都有,但不是全部都有,只有百分之几。 您需要使用以下公式计算每个品牌的百分比:

(所有产品的数量 (id) - 不可用的产品数量 (maxItems = 0)) / count(id) * 100% = 结果 %

请求示例如下:

SELECT brand, 
    (((SELECT COUNT(1) FROM products) -
    (SELECT COUNT(1) FROM products WHERE maxItems = 0)) / 
    (SELECT COUNT(1) FROM products) * 100) as cnt
    FROM products
        WHERE cnt > 85
    GROUP BY brand
    ORDER BY cnt DESC
    LIMIT 1000;

结果:

brand   cnt
Amorem  99.27102236131287
VALENS  99.27102236131287
FARFAL  99.27102236131287
VIAILA  99.27102236131287
4Kids   99.27102236131287

我应该在代码中修正什么才能仅按品牌计算百分比? 谢谢。

SELECT brand, (count() - countIf(maxItems = 0)) / count() * 100 as cnt
    FROM products
    GROUP BY brand
    HAVING cnt > 85
    ORDER BY cnt DESC
    LIMIT 1000;