I have:
TABLE1
STUDENT CLASS SCORE
PAT A 14
PAT A 10
PAT B 13
PAT B 20
PAT C 11
MARK A 18
MARK A 13
MARK A 17
MARK A 10
DAVIS C 12
JER A 15
JER B 12
JER C 20
JER C 17
BARB C 14
BARB C 19
BARB C 11
TOM A 18
TOM A 12
TOM B 16
TOM B 12
and wish for to make:
STUDENT CLASS AVERAGESCORE
PAT A 12
PAT B 16.5
PAT C 11
DAVIS A 14.5
DAVIS B NA
DAVIS C NA
MARK A NA
MARK B NA
MARK C 12
JER A 15
JER B 12
JER C 18.5
Which does this: for the STUDENT in the list, calculate AVERAGE(SCORE) BY CLASS AND STUDENT
I try this with no success:
LIST1 = PAT, MARK, DAVIS, JER
select STUDENT, CLASS, AVG(SCORE) OVER(PARTITION BY STUDENT, CLASS) AS AVERAGE_SCORE
from TABLE1
where STUDENT in LIST1
select student, class, avg(score)
from table1
where student in ('PAT', 'DAVIS', 'MARK', 'JER')
group by student, class
Try the following, you first need a list of all class / student combinations, to which you can outer-join and then aggregate:
with c as (select distinct class from t),
s as (select Student from(values('PAT'),('MARK'),('DAVIS'),('JER'))s(Student)),
a as (select Student, Class from s cross join c)
select a.Student, a.Class, Avg(t.Score * 1.0) AverageScore
from a
left join t on t.class = a.class and t.Student = a.Student
group by a.Student, a.Class
order by a.Student, a.Class;
Related
I have 3 tables:
table: a
id name
2 A
3 B
table: b
id a b
3 2 Asd Ter Gsdt
4 2 Gsd Gsdt Gsda
5 2 Asd Gsd
6 3 Uty Kggs
7 3 Tyud Hffddf
table: c
id a b
6 3 d
7 3 a
8 3 g
9 3 h
10 4 j
11 5 y
12 5 s
13 6 d
14 6 h
expected output:
a b c d
A 2019-04-06 3 a
B 2019-04-06 6 b
I am unsure how to proceed from this, how?
This query do the job, but there is always a question about speed and performance.
select a.name,
(select c_date from c
join b on (c.b_id = b.id)
where b.a = a.id order by c_date desc limit 1) last_c_date,
popular.b_id,
(select photos->0 from b where id = popular.b_id) photo
from a
join (
select distinct on (a)
b.id b_id, a from b
join c on (b.id = c.b_id)
group by b.id, a
order by a, count(*) desc, b.id
) popular on (popular.a = a.id)
order by a.name
If there will be 2 equaly popular b objects in a region, query takes this with smaller id.
If it will be no b object with entries in c than subquery for photo can be surrounded with coalesce (but now it should work too with null value).
so I have the following table:
personID grade classID
13 7 147
13 7 456
19 8 123
19 8 789
25 7 123
25 7 456
25 7 789
82 8 147
82 8 456
155 7 456
155 7 789
I would like to keep the grade column, but have the personID and classID be mapped to another table to get the personName and ClassName - each are different tables, but the one separate table with the person name also has person ID, and the one with the class name has the classID so it makes it easy.
The result table should still be 11 columns long, just replaced with the persons name and class name for each row.
Is this done with a join?
Yes. The base idea is:
select
p.personID,
p.personName,
g.grade,
c.classID,
c.className
from grades g
inner join persons p on p.personID = g.personID
inner join classes c on c.classID = g.classID
This assumes the following data structures:
persons:
personID
personName
classes:
classID
className
grades:
personID --> foreign key to persons(personID)
grade
classID --> foreign key to classes(classID)
Select * from
gradeTable g
JOIN
personTable p
ON g.PersonID = p.PersonID
JOIN
classTable c
ON g.ClassID = c.ClassID
Currently I have tables like these:
Table Customer
ID Name
01 Angel
02 Charlie
Table Input1
Name Value
Charlie 5
Angel 7
Charlie 10
Angel 3
Table Input2
Name Value
Charlie 10
Angel 5
Charlie 15
Angel 25
How to get result like this
Name Input1 Input2
Angel 10 30
Charlie 15 25
Try this:
SELECT
c.Name,
i1."Value" value1,
i2."Value" value2
FROM
Customer c LEFT JOIN
(SELECT Name, SUM("Value") "Value" from input1 group by Name) i1
ON c.Name = i1.Name LEFT JOIN
(SELECT Name, SUM("Value") "Value" from input2 group by Name) i2
ON c.Name = i2.Name
DEMO
http://sqlfiddle.com/#!9/204220/2
Table A:
NAME ID
---------------
Peter 45
Johny 46
Mary 47
Micheal 48
Elizabeth 49
Table B:
NAME ID Gender
-----------------------------
Peter 12 M
Johny null M
Mary 47 F
Micheal 48 M
Elizabeth 28 F
I want an update query to populate the below result in Table B.
NAME ID Gender
-----------------------------
Peter 45 M
Johny 46 M
Mary 47 F
Micheal 48 M
Elizabeth 49 F
Assuming Name is unique, here is a starting point depending upon your RDBMS:
update b
set b.ID = a.ID
from TableA a
join TableB b on a.Name = b.Name
where a.ID <> b.ID
or b.ID is null
I have a SQL database with two tables
TableA (ID, State, Value)
1 England 20
2 France 50
3 USA 40
4 ........
5 ........
and
TableB (ID, username, age, stateID)
1 John 15 1
2 Adam 20 2
3 Jane 40 3
4 Scott 50 1
5 Edwin 60 2
6 Alex 20 3
7 Olsen 30 1
8 ...........
9 ...........
What I need is to update TableB by setting the age for all users
from England to be 20
and from France to be 50
and so on...
update tableB
set age = (select tableA.value from tableA where tableA.StateID=TableB.id)
I like this form below:
update b set
age = a.value
from tableB b
join tableA a on a.id = b.stateId
because you can write it this way (at last in SQL Server Management Studio):
update b set
age = a.value
--select b.age, a.value, b.*, a.*
from tableB b
join tableA a on a.id = b.stateId
then highlight the part from select ... to the end of query and execute it (F5) to check what you are going to change (value before and after).