Skip to content
Advertisement

How to get distinct parent_sku count for the below dataset?

Need your help to extract distinct Parent_sku count from the below data set.

Condition: If one child of a parent_sku has “High” sales_tag then that parent_sku count should be excluded from “Low” sales_tag.

P.S. Sales_tag column is based on child_sku column.

Thank you for your help.

Dateset_&_Output

Advertisement

Answer

The presented schema is non-conducive to this type of query, however we can normalise the structure to extract a parent and the child rows based on the Child_sku value.

Then we can LEFT JOIN the parent to the child row to record the count.

NOTE:
This specific query will not likely return the correct counts if there are more than 1 single child for each parent, it is not clear from the instructions how that should be treated though, so it mnight give you what you need.

Brand Sales_Tag P_SKU_Count
Nike High 3
Nike Low 2

You could also avoid the CASE in the GROUP BY statement and return the two counts in-line:

Brand High Low
Nike 3 2

There is a DB Fiddle here to test with: https://www.db-fiddle.com/f/s2hDvn8EU3QXcdwKqobhdc/0

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