How to assign groups based on sum of values in oracle?

by raven_corwin , in category: MySQL , 7 months ago

How to assign groups based on sum of values in oracle?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 7 months ago

@raven_corwin 

To assign groups based on the sum of values in Oracle, you can use the CASE statement in conjunction with the SUM() function. Here is an example query that demonstrates how to assign groups based on the sum of values:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
SELECT
    *,
    CASE
        WHEN total_sum < 100 THEN 'Group A'
        WHEN total_sum >= 100 AND total_sum < 200 THEN 'Group B'
        WHEN total_sum >= 200 THEN 'Group C'
    END AS group_assignment
FROM (
    SELECT
       id,
       SUM(value) AS total_sum
    FROM
       your_table
   GROUP BY
       id
) subquery;


In this query, we first calculate the sum of values for each group using the SUM() function in a subquery. Then, in the outer query, we use the CASE statement to assign a group based on the total sum of values. You can adjust the logic in the CASE statement to define the groups based on your specific requirements.

Related Threads:

What values can be assign to "x-nginx-cache" in .htaccess file?
How to use group by to sum values in mysql?
How to get sum of values by grouping using laravel?
How to assign value to oracle cursor while iterating?
How to find the sum that is less than 1000 in oracle?
How to get sum of count of the columns in oracle?