I have a list of products and a count corresponding to the quantity sold in a single table. The data is laid out as such:
Product Name QTY_SOLD
Mouse 23
Keyboard 25
Monitor 56
TV 10
Laptop 45
...
I want to create a group ID where groups are broken up if the sum of the quantity sold is greater than 50. We can order by Product Name to get an output similar to the following.
Product Name QTY_SOLD GROUP_NBR
Keyboard 25 1
Laptop 45 1
Monitor 56 2
Mouse 23 3
TV 10 3
I created a case statement to create the output I need but if I want to change the group id cutoff from 50 to say 100 or if i get more products and quantities I have to keep changing the case statement. Is there an easy way to use either recursion or some other method to accomodate this?
UPDATE main FROM prod_list AS main, ( SEL PROD_NAME , QTY_SOLD , SUM(QTY_SOLD) OVER (ORDER BY PROD_NAME ROWS UNBOUNDED PRECEDING) RUNNING FROM prod_list ) inr SET GROUP_NBR = CASE WHEN RUNNING < 50 THEN 1 WHEN RUNNING > 50 AND RUNNING < 100 THEN 2 WHEN RUNNING > 100 AND RUNNING < 150 THEN 3 WHEN RUNNING > 150 AND RUNNING < 200 THEN 4 WHEN RUNNING > 200 AND RUNNING < 250 THEN 5 ELSE 6 END WHERE main.PROD_NAME = inr.PROD_NAME ;