

How to choose among the sum of the grouped numbers of the table above their sum...
source link: https://www.codesd.com/item/how-to-choose-among-the-sum-of-the-grouped-numbers-of-the-table-above-their-sum-average-in-oracle.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to choose among the sum of the grouped numbers of the table above their sum average in Oracle?
I have data like this:
tableA.ID
---------
1
2
3
tableB.ID tableB.NUM
--------------------
1 10
1 15
2 18
3 12
2 12
2 15
3 13
1 12
I need to select tableA IDs where the sum of their NUMs in tableB is above the average of all tableA IDs sums. In other words:
SUM ID=1 -> 10+15+12 = 37
SUM ID=2 -> 18+12+15 = 45
SUM ID=3 -> 12+13 = 25
AVG ALL IDs -> (37+45+25)/3 = 35
The SELECT must only show ID 1 and 2 because 37 > 35, 45 > 35 but 25 < 35.
This is my current query which is working fine:
SELECT tableA.ID
FROM tableA, tableB
WHERE tableA.ID = tableB.ID
HAVING SUM(tableB.NUM) > (
SELECT AVG(MY_SUM)
FROM (
SELECT SUM(tableB.NUM) MY_SUM
FROM tableA, tableB
WHERE tableA.ID = tableB.ID
GROUP BY tableA.ID
)
)
GROUP BY tableA.ID
But I have a feeling there might be a better way without all those nested SELECTs. Perhaps 2, but 3 feels like too much. I'm probably wrong though.
For instance, why can't I do something simple like this:
SELECT tableA.ID
FROM tableA, tableB
WHERE tableA.ID = tableB.ID
HAVING SUM(tableB.NUM) > AVG(SUM(tableB.NUM))
GROUP BY tableA.ID
Or this:
SELECT tableA.ID, SUM(tableB.NUM) MY_SUM
FROM tableA, tableB
WHERE tableA.ID = tableB.ID
HAVING MY_SUM > AVG(MY_SUM)
GROUP BY tableA.ID
In SQL Server you can do this. Don't know if it works in Oracle. I'm sure I'll find out soon if it doesn't!
WITH cte As (
SELECT
tableA.ID,
SUM(tableB.NUM) AS MY_SUM,
AVG(SUM(tableB.NUM)) over() As Average
FROM tableA, tableB
WHERE tableA.ID = tableB.ID
GROUP BY tableA.ID
)
SELECT ID
FROM cte
WHERE MY_SUM > Average
Recommend
-
52
As a follow-up on myprevious post about alternate row colors in a ListView, this question was posted on StackOverflow. The po...
-
43
Introduction In SQL Server 2012, grouped (vector) aggregation was able to use parallel batch-mode execution, but only for the partial (per-thread) aggregate. The associated global aggregate always ran in row m...
-
20
This week we got a big improvement in how the Task Manager handles grouped tasks: by default, it activates the last-used task and then cycles through other tasks if you continue to click on it. There are also some more we...
-
13
Xamarin forms – UWP fix listview grouped header height problem When UWP comes into play with Xamarin Forms, you’ll notice that several default control settings are often not resulting in a pleasing UI. One that caught my eye r...
-
13
Leetcode Patterns Table of Contents Background This repo is intended for any individual wanting to improve their problem solving skills for software engineering interviews. Problems are grouped under their r...
-
12
Leetcode Patterns Table of Contents Background This repo is intended for any individual wanting to improve their problem solving skills for software engineering interviews. Problems are grouped under their r...
-
7
Sorting nodes grouped by taxonomy term advertisements Ok, here's the problem: I have a list of contacts, which i have created in...
-
10
A list of all Visual Studio Code customizable colors, grouped by UI region. Copy and paste into User Settings (comments are allowed) to tweak an existing theme or work on your own. · GitHub ...
-
11
Sample 69804: Side-by-side grouped bar chart and pie chart This SAS Note provides sample code to use the Graph Template Language, GTL, and the SGRENDER procedure to create a graph that contains a bar chart an...
-
7
Yasho Ratna Gupta May 10, 2023 5 minute read...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK