SQL Query -- AVG for Occurrence Across Multiple Columns - sql

I have a table like the following
ID_A
ID_B
Avg_Class_Size
1
2
16
3
4
10
2
3
8
2
4
9
Where ID_A and ID_B represent distinct student ID codes, and AVG_Class_Size represents the average class size of the classes shared between students A and B.
I would like to calculate the average of the "avg_class_size" for each student, regardless of whether they are student "A" or student "B", with results like below:
ID
AVG
1
16
2
11
3
9
4
9.5
Is there a simple way to accomplish this with a SQL query?

Select with UNION ALL all the ids and averages of the students and aggregate:
SELECT ID, AVG(Avg_Class_Size) average
FROM (
SELECT ID_A ID, Avg_Class_Size FROM tablename
UNION ALL
SELECT ID_B ID, Avg_Class_Size FROM tablename
) t
GROUP BY ID
See the demo.
Results:
ID
average
1
16
2
11
3
9
4
9.5

Related

SUM a column in SQL, based on DISTINCT values in another column, GROUP BY a third column

I'd appreciate some help on the following SQL problem:
I have a table of 3 columns:
ID Group Value
1 1 5
1 1 5
1 2 10
1 2 10
1 3 20
2 1 5
2 1 5
2 1 5
2 2 10
2 2 10
3 1 5
3 2 10
3 2 10
3 2 10
3 4 50
I need to group by ID, and I would like to SUM the values based on DISTINCT values in Group. So the value for a group is only accounted for once even though it may appear multiple for times for a particular ID.
So for IDs 1, 2 and 3, it should return 35, 15 and 65, respectively.
ID SUM
1 35
2 15
3 65
Note that each Group doesn't necessarily have a unique value
Thanks
the CTE will remove all duplicates, so if there a sdiffrenet values for ID and Group, it will be counted.
The next SELECT wil "GROUP By" ID
For Pstgres you would get
WITH CTE as
(SELECT DISTINCT "ID", "Group", "Value" FROM tablA
)
SELECT "ID", SUM("Value") FROM CTE GROUP BY "ID"
ORDER BY "ID"
ID | sum
-: | --:
1 | 35
2 | 15
3 | 65
db<>fiddle here
Given what we know at the moment this is what I'm thinking...
The CTE/Inline view eliminate duplicates before the sum occurs.
WITH CTE AS (SELECT DISTINCT ID, Group, Value FROM TableName)
SELECT ID, Sum(Value)
FROM CTE
GROUP BY ID
or
SELECT ID, Sum(Value)
FROM (SELECT DISTINCT * FROM TableName) CTE
GROUP BY ID

How to sum values in a column based on values in a different column SQL

For example, lets say I have
id
values
1
10
1
12
1
10
2
2
2
5
2
4
then i would want sql to return
id
values
1
32
2
11
This is very basic sql.
select id, sum(values) as values
from foo
group by id
Select ID,sum(values)
From table
Group by ID;

How do i select 4 rows from a table holding 10 rows of persons based on the youngest age?

Lets say i have a table containing these rows
id age
1 5
2 7
3 8
4 9
5 3
6 1
How do i write a select statement that selects exactly 4 of the youngest persons?
You would use top and order by:
select top (4) t.*
from t
order by age asc;
SELECT id, age
FROM [MyTable]
ORDER BY age
OFFSET 0 ROWS FETCH NEXT 4 ROWS ONLY

sum of 2 different columns in MSSQL

I have 2 tables A and B. The columns names are similar in both the tables. The columns are
1. fees
2. user_id
I want to get the sum of fees from both tables where user_id = 1
For eg:
Table A:
id user_id fees
1 1 10
2 2 11
3 1 5
Table B:
id user_id fees
1 1 15
2 2 10
3 1 20
I need the result as below:
user_id fees
1 50
2 21
Please help me with the query
Try this:
select user_id, sum(fees) from (
select user_id, fees from Table_A
union all
select user_id, fees from Table_B
) as A
group by user_id

Select unique subsets

I have a table like in example below.
SQL> select * from test;
ID PARENT_ID NAME
1 1 A
2 1 B
3 2 A
4 2 B
5 3 A
6 3 B
7 3 C
8 4 A
What I need is to get all unique subsets of names ((A,B), (A,B,C), (A)) or exclude duplicate subsets. You can see that (A,B) is twice there, one for PARENT_ID=1 and one for 2.
I want to exclude such duplicates:
ID PARENT_ID NAME
1 1 A
2 1 B
5 3 A
6 3 B
7 3 C
8 4 A
You can use DISTINCT to only return different values.
e.g.
SELECT DISTINCT GROUP_CONCAT(NAME SEPARATOR ',') as subsets
FROM TABLE_1
GROUP BY PARENT_ID;
SQL Fiddle
I have used 'group_concat' assuming you are using 'Mysql'. The equivalent function in Oracle is 'listagg()'. you can see it in action here in SQL fiddle
Here is the solution:-
Select a.* from
test a
inner join
(
Select nm, min(parent_id) as p_id
from
(
Select Parent_id, group_concat(NAME) as nm
from test
group by Parent_ID
) a
group by nm
)b
on a.Parent_id=b.p_id
order by parent_id, name