通过条件语句进行分区

mar*_*2x4 4 sql database analysis snowflake-cloud-data-platform

我有各个商店销售的产品的数据。对于某些商店,它们以 映射的折扣出售PROMO_FLG。我想显示两COUNT PARTITION列。

+-------------------------+--------------+---------------------+
| Store                   | Item         | PROMO_FLG|
|-------------------------+--------------+---------------------|
| 1                       |            1 |                   0 |
| 2                       |            1 |                   1 |
| 3                       |            1 |                   0 |
| 4                       |            1 |                   0 |
| 5                       |            1 |                   1 |
| 6                       |            1 |                   1 |
| 7                       |            1 |                   1 |
| 8                       |            1 |                   0 |
| 9                       |            1 |                   0 |
| 10                      |            1 |                   0 |
+-------------------------+--------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

首先显示所有有该产品的商店(已完成)

COUNT(DISTINCT STORE) OVER (PARTITION ITEM)给出的是 10

第二个 - 我寻求的 - 仅计算那些具有属性价值的商店PROMO_FLG = 1

这应该给我们带来价值4

Gor*_*off 5

我想你想要:

select t.*,
       count(*) over (partition by item) as num_stores,
       sum(promo_flg) over (partition by item) as num_promo_1
from t;
Run Code Online (Sandbox Code Playgroud)

如果您确实需要不同的计数:

select t.*,
       count(distinct store) over (partition by item) as num_stores,
       count(distinct case when promo_flg = 1 then store end) over (partition by item) as num_promo_1
from t;
Run Code Online (Sandbox Code Playgroud)

是一个 db<>fiddle。fiddle 使用 Oracle 因为它支持COUNT(DISTINCT)窗口函数。

如果窗口函数不起作用,这里有一个替代方案:

select *
from t join
     (select item, count(distinct store) as num_stores, count(distinct case when promo_flg = 1 then store end) as num_stores_promo
      from t
      group by item
     ) tt
     using (item);
Run Code Online (Sandbox Code Playgroud)