I have a sample table in snowflake database which has details about the product bought, the code of the shop, date on which the product was purchased, the amount of the product and the type of business as shown below:
From this table, I need to remove the duplicates based on the columns "Shop_Code" and "Purchase_Date". I need to also keep in mind of the following conditions while removing the duplicates. Those are:
- If we get two rows while grouping on "Shop_Code" and "Purchase_Date" where the "Business_Type" is both "Large" then any one of the row can be deleted.
- If we get two rows while grouping on "Shop_Code" and "Purchase_Date" where the "Business_Type" is both "MidSmall" then any one of the row can be deleted.
- If we get one row with the "Business_Type" as "Large" and other one as "MidSmall" while grouping on "Shop_Code" and "Purchase_Date" then in this case the row with the "Business_Type" as "MidSmall" should be deleted retaining the row with "Large" as the business type.
The output I'm expecting is that rows 1, 3, 5 and 8 should be present in the output table. Request your help in terms of Snowflake SQL.