Skip to content
Advertisement

Partition by with condition statement

I have data of products that are sold by various shops. For some shops they are sold with discount mapped by PROMO_FLG. I would like to display two COUNT PARTITION columns.

First displays all shops that thave this product (which is done)

COUNT(DISTINCT STORE) OVER (PARTITION ITEM) would give is 10

Second one – which I seek – counts only these shops that have value in PROMO_FLG = 1 attribute.

That should give us value of 4

Advertisement

Answer

I think you want:

If you actually need distinct counts:

Here is a db<>fiddle. The fiddle uses Oracle because it supports COUNT(DISTINCT) as a window function.

Here is an alternative, if the window functions don’t work:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement