I have the following two tables
TeamsTable
TeamID
TeamName
1
Name1
2
Name2
3
Name3
...
...
and
GameScoresTable
GameID
HomeTeam(FK)
AwayTeam(FK)
HomeTeamScore
AwayTeamScore
1
1
2
50
60
2
2
3
70
80
3
3
4
70
80
...
...
...
...
...
I want to get a table like this:
FinalTable
GameID
HomeTeam
AwayTeam
TotalScore
1
Name1
Name2
110
2
Name2
Name3
150
3
Name3
Name4
150
...
...
...
...
I tried the following query, but it doesn't work.
SELECT
GameScores.GameID
,TeamH.TeamName as HomeTeam
,TeamA.TeamName as AwayTeam
,SUM(GameScores.HomeTeamScore + GameScores.AwayTeamScore)
FROM GameScores
INNER JOIN Teams TeamH ON GameScores.HomeTeam=TeamH.TeamID
INNER JOIN Teams TeamA ON GameScores.AwayTeam=TeamA.TeamID
GROUP BY GameID
Essentially, I want to get the HomeTeam and AwayTeam columns to show their proper names rather than the foreign key value and want the last column to show their combined score.
You don't want to aggregate your data. Just use + to get the sum.
SELECT
GameScores.GameID
,TeamH.TeamName AS HomeTeam
,TeamA.TeamName AS AwayTeam
,GameScores.HomeTeamScore + GameScores.AwayTeamScore AS totalscore
FROM GameScores
INNER JOIN Teams TeamH ON GameScores.HomeTeam=TeamH.TeamID
INNER JOIN Teams TeamA ON GameScores.AwayTeam=TeamA.TeamID
Related
I have three tables: users, products and reviews. I'm trying to get form a table that would show products that have gotten the same review from different users, the users who reviewed it and what review it got.
Here are the tables and the output I'm looking for:
Users
uid uname
1 name1
2 name2
3 name3
4 name4
Products
pid pname
1 A
2 B
3 C
4 D
Reviews
pid uid grade
1 1 3
1 2 2
1 3 3
2 1 4
3 2 1
2 2 4
4 3 1
Desired output:
uname uname2 pname grade
name1 name3 A 3
name3 name1 A 3
name1 name2 B 4
name2 name1 B 4
There are some overly complicated answers here.
Its pretty simple using a self join like this:
select u1.uname, u2.uname, p.pname, r1.grade
from review r1
join review r2 on r2.pid=r1.pid and r2.grade=r1.grade and r2.uid<>r1.uid
join products p on p.pid=r1.pid
join users u1 on u1.uid=r1.uid
join users u2 on u2.uid=r2.uid
order by pname, r1.grade, u1.uname, u2.uname
Result:
uname uname1 pname grade
name1 name3 A 3
name3 name1 A 3
name1 name2 B 4
name2 name1 B 4
select users.uname, reviews.grade, products.pname from products
join reviews on products.id = reviews.pid
join users on users.id = reviews.id
This can be done using a self-join like this -
with combined as
(select (select uname from users where uid = r.uid),
(select pname from products where pid = r.pid),
r.grade,
r.uid
from reviews r)
select c1.uname as name1, c2.uname as name2, c1.pname, c1.grade
from combined c1, combined c2
where c1.grade = c2.grade
and c1.pname = c2.pname
and c1.uid <> c2.uid;
I have a TableA:
TeamID PersonID
... ...
1 100
2 101
2 105
2 444
3 501
... ...
Also TableB
PersonID CourseID
... ...
444 c103
444 c2048
101 c3214
... ...
How do I write a simple query such that, for example, I can find whether each person in Team 2 has at least one CourseID associated in TableB. The result for my example is:
TeamID PersonID HasCourse
2 101 1
2 105 0
2 444 1
The shorter the query the better, ideally not using loops.
I would just use case exists:
select a.*,
(case when exists (select 1 from b where b.personid = a.personid)
then 1 else 0
end) as hascourse
from a
where team_id = 2;
Just another method.
select a.TeamID, a.PersonID,
iif(count(b.CourseID)>0,1,0) as HasCourse
from TableA a
left join TableB b on b.PersonID = a.PersonID
where a.TeamID = 2
group by a.TeamID, a.PersonID;
I have a two separate tables, one with vacancies, and one with applications to those vacancies. I want to select a new table which selects from the vacancies table with a number of other columns from that table, and another column that calculates how many applications there are for those vacancies. So my vacancy table looks like this:
ID Active StartDate JobID JobTypeID HoursPerWeek
1 1 2017-02-28 2 CE 0
2 1 2017-02-15 4 CE 40
3 1 2017-02-14 1 CE 40
4 1 2017-02-28 1 CE 48
My applications table looks like this:
ID VacancyID Forename Surname EmailAddress TelephoneNumber
1 1 John Smith jsmith#gmail.com 447777777777
2 2 John Smith jsmith#gmail.com 447748772641
3 2 John Smith jsmith#gmail.com 447777777777
4 2 John Smith jsmith#gmail.com 447700123456
5 4 John Smith jsmith#gmail.com 447400123569
6 4 John Smith jsmith#gmail.com 447400126547
7 4 John Smith jsmith#gmail.com 447555123654
I want a table that looks like this:
ID Active StartDate JobID HoursPerWeek NumberOfApplicants
1 1 2017-02-28 2 0 1
2 1 2017-02-15 4 40 3
3 1 2017-02-14 1 40 0
4 1 2017-02-28 1 48 3
How can I select that table using joins and count the number of applicants where the VacancyID is equal to the ID of the first vacancy table? I have tried:
select Vacancy.ID, VacancyID, count(*) as NumberOfApplications from VacancyApplication
join Vacancy on Vacancy.ID=VacancyID
group by VacancyID, Vacancy.ID
This obviously doesn't select all the other columns and it also does not select ID 3 because there are 0 applications for that - I want ID 3 to be there with a value of 0 as well as all the other columns. How do I do this? I've tried various forms of grouping and selecting but I'm quite new to SQL so I'm not really sure how this can be done.
Use RIGHT JOIN instead of INNER JOIN and count the vacancyid column from vacancyapplication table. For the non matching records you will get count as 0
SELECT v.id, v.Active, v.StartDate, v.JobID, v.HoursPerWeek
Count(va.vacancyid) AS NumberOfApplications
FROM vacancyapplication va
RIGHT JOIN vacancy v
ON v.id = va.vacancyid
GROUP BY v.id, v.Active, v.StartDate, v.JobID, v.HoursPerWeek
Start using Alias names, it makes the query more readable
Hoping, i understood your problem correctly. Please try below query
select Vacancy.ID, VacancyID, count(*) as NumberOfApplications from VacancyApplication
left join Vacancy on Vacancy.ID=VacancyID
group by VacancyID, Vacancy.ID
You can use count as a window function using the OVER clause, thus eliminating he need for group by:
SELECT v.ID,
v.Active,
v.StartDate,
v.JobID,
v.JobTypeID,
COUNT(va.ID) OVER(PARTITION BY v.ID) HoursPerWeek
FROM Vacancy v
LEFT JOIN vacancyapplication va ON(v.ID = va.VacancyID)
Use left join and table aliases:
select v.ID, count(va.VacancyID) as NumberOfApplications
from Vacancy v join
VacancyApplication va
on v.ID = va.VacancyID
group by v.ID;
You seem to want all the columns. You could include them in the group by. However, a correlated subquery or outer apply is simpler:
select v.*, va.cnt
from vacancy v outer apply
(select count(*) as cnt
from VacancyApplication va
where v.ID = va.VacancyID
) va;
This is probably more efficient anyway, especially if you have an index on VacancyApplication(VacancyID).
I have the following tables
goal matches team_match
id | match_id | team_id | name match_id team_id | match_id | home_away
----------------------------- ----- ------------------------------
1 1 1 Ronaldo 1 1 1 home
2 1 2 Messi 2 1 away
3 1 2 Suarez 2 1 away
Now I want to get just players who played in the away team.
SELECT DISTINCT g.name
FROM goal g
INNER JOIN matches m
ON g.match_id = m.match_id
INNER JOIN team_match tm
ON tm.match_id = m.match_id
AND tm.home_away = 'away'
WHERE m.match_id = '1'
But instead of:
Messi
Suarez
I am getting:
Ronaldo
Messi
Suarez
It seams like my second INNER JOIN is totally ignored, even if I change from "home" to "away" I will get the same result.
The INNER JOIN is working correctly. Seems your database schema is the problem.
Why shouldn't Ronaldo be in the result set? You don't use the team_id anywhere. There is no information that Ronaldo was in the team that played home in the specified match.
A suggestion for a better schema:
Table teams
team_id
1
2
Table players:
playerid team_id name
1 1 Ronaldo
2 2 Messi
3 2 Suarez
Table matches
matchid hometeamid awayteamid
1 1 2
Table goals
matchid playerid
1 1
1 2
1 3
Then your query could look like this
SELECT p.Name FROM players p
INNER JOIN goals g ON (g.playerid = p.playerid)
INNER JOIN matches m ON (m.matchid = g.matchid AND m.awayteamid = p.team_id)
WHERE m.matchid = 1
You haven't proven that the first inner join is working either.
Try removing the blank lines
Let's say I have two tables. A students table and an observations table. If the students table looks like:
Id Student Grade
1 Alex 3
2 Barney 3
3 Cara 4
4 Diana 4
And the observations table looks like:
Id Student_Id Observation_Type
1 1 A
2 1 B
3 3 A
4 2 A
5 4 B
6 3 A
7 2 B
8 4 B
9 1 A
Basically, the result I'd like from the query would be the following:
Student Grade Observation_A_Count
Alex 3 2
Barney 3 1
Cara 4 2
Diana 4 0
In other words, I'd like to gather data for each student from the students table and for each student count the number of A observations from the observations table and tack that onto the other information. How do I go about doing this?
This is a simple join and aggregate:
select
a.Student,
a.Grade,
count(b.Id) as Observation_A_Count
from
Student a left join
Observations b on a.Id = b.Student_Id
group by
a.Student,
a.Grade
order by
1
Or, you can use a correlated subquery:
select
a.Student,
a.Grade,
(select count(*) from observations x where x.Student_Id = a.Id) as Observation_A_Count
from
Student a
order by
a.Student
You can join the table with a specific condition, by doing this you can have a field for Observation_B_Count and Observation_C_Count, etc.
SELECT Student.Student, Student.Grade, COUNT(Observation_Type.*) AS Observation_A_Count
FROM Student
LEFT JOIN Observations ON Observations.Student_ID = Student.Student_ID AND Observations.Observation_Type = 'A'
GROUP BY Student.Student, Student.Grade