Skip to content
Advertisement

sql (oracle) count and sum within the same select/where query

With reference to the following and correctly answered question:
sql (oracle) counting number of overlapping intervals

Given the following table test in an oracle sql database:

I would now like to find the number of overlapping intervals (endpoints included) [start, stop] n_overlap as well as the sum of the stop values for all id having the same name, i.e.:

I tried this solution, which works:

But, is there a way to condense the two select/where queries, using e.g.:

which raises a too many values error?

Advertisement

Answer

You should be able to do what you want with a JOIN and GROUP BY:

Output:

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