Search code examples
sqldatabasesqlitesumwindow-functions

Choosing 10 largest sets of data based on sum, outputting cumulative sum for each


Say the dataset is:

Class    Value    Ordering
A        10       1
A        13       2
...
B        20       1
B        7        2
...

I want to be able to find the 10 classes with the highest total value and then output the cumulative sum of each class.

So far I have created a script to determine the 10 largest:

SELECT Class
FROM Table
GROUP BY Class
ORDER BY sum(Value) DESC
LIMIT 10;

And a script to find the cumulative sum of a specific class:

SELECT sum(Value) OVER (
       ORDER BY Ordering
       ROWS BETWEEN
           UNBOUNDED PRECEDING
           AND CURRENT ROW
       ) AS cumulativeSum
FROM Table
WHERE Class = 'A'
ORDER BY Ordering ASC;

But I cannot find a way to combine the process together

EDIT:

Assuming A and B were two of the highest classes, the output would be:

A    B
10   20
23   27

If a class C was not one of the 10 largest, it would not be output


Solution

  • If I followed you correctly, you can do:

    select class, value, ordering, cumulativeSum
    from (
        select
            t.*,
            rank() over(order by totalsum desc) rn
        from (
            select 
                t.*, 
                sum(value) over(partition by class order by ordering) cumulativeSum,
                sum(value) over(partition by class) totalsum
            from table t
        ) t
    ) t
    where rn <= 10
    order by class, ordering
    

    This filters the table on the top 10 classes by their total value, and adds a cumulative sum per class to each row.