sqlite Joins with MAX - sql

I have 2 tables. One displays a game played (Date,Where, result,opponent etc) and the other one the details of a batting innings (runs scored, etc) Both tables have a primary key that relates the batting back to a specific game.
I am trying to return the OPPONENT column from Games when the MAX (highest) score is recorded in the table BATTING, but currently i am unsure how to do this.
The 2 tables can be found here
http://i.imgur.com/bqiyD3X.png
The example from these tables would be (max score is 101 in RUNSSCORED, so return the linked OPPONENT from GAMEINDEX which is "Ferndale"
Any help would be great. Thanks.

Is this what you are looking for?
select OPPONENT
from GAMES
where GAMESINDEX in
(select GAMESINDEX from BATTING order by RUNSSCORED desc limit 1);
If there isn't a unique max RUNSSCORED value, then the answer might not be deterministic.
If you want multiple winners in that case, you could use
select OPPONENT
from GAMES natural join BATTING
WHERE RUNSSCORED in (select MAX(RUNSSCORED) from BATTING);

SELECT G.OPPONENT, MAX(B.RUNSSCORED)
FROM GAMES AS G
INNER JOIN BATTING AS B
ON G.GAMESINDEX = B.GAMESINDEX

Related

Finding total number of points scores by each team, but relying on foreign relations

I am trying to craft a SQL query to solve the following problem. In the Game table, you see that there is away_team_id and home_team_id. Essentially, I want to find the total number of points for the away and home team for the specific game id.
In order to find the total points, you have to find the players from the respective team that participated in that specific game id. From there, you can sum their points, which would give the total number of points scored by that specific team.
However, I've relied on foreign keys to keep my database maintainable, which has made it a little difficult for me to compute these things. Does anyone mind showing me how I can accomplish this?
For example, I can do this to find the sum of only the away team or home team. I'd ideally want both results in one query. In the following example, 1 is the id of game and 2 is the id of the away team. This will give me the sum of points scored by the away team in game id 1. I can write another query and use the id of the home team, so I can accomplish this in two queries. However, how can I do this in one?
SELECT SUM(statistic.total_points)
FROM player, team, statistic
WHERE player.team_id = team.team_id
AND statistic.player_id = player.player_id
AND statistic.game_id = 1
AND team.team_id = 2
Game:
game_id
away_team_id
home_team_id
Statistic:
game_id
player_id
total_points
Team:
team_id
name
Player:
player_id
team_id
Thanks.
I think you can calculate the statistics per team per game and then join to this table --
WITH gamestats AS (
SELECT s.game_id, p.team_id, SUM(p.total_points) as total_points
FROM statistic s
INNER JOIN player p
ON s.player_id = p.player_id
GROUP BY s.game_id, p.team_id
)
SELECT g.game_id, ISNULL(away.total_points, 0), ISNULL(home.total_points, 0)
FROM game g
LEFT JOIN gamestats away
ON g.game_id = away.game_id
AND g.away_team_id = away.team_id
LEFT JOIN gamestats home
ON g.game_id = home.game_id
AND g.home_team_id = home.team_id

SQL Join query brings multiple results

I have 2 tables. One lists all the goals scored in the English Premier League and who scored it and the other, the squad numbers of each player in the league.
I want to do a join so that the table sums the total number of goals by player name, and then looks up the squad number of that player.
Table A [goal_scorer]
[]1
Table B [squads]
[]2
I have the SQL query below:
SELECT goal_scorer.*,sum(goal_scorer.number),squads.squad_number
FROM goal_scorer
Inner join squads on goal_scorer.name=squads.player
group by goal_scorer.name
The issue I have is that in the result, the sum of 'number' is too high and seems to include duplicate rows. For example, Aaron Lennon has scored 33 times, not 264 as shown below.
Maybe you want something like this?
SELECT goal_scorer.*, s.total, squads.squad_number
FROM goal_scorer
LEFT JOIN (
SELECT name, sum(number) as total
FROM goal_scorer
GROUP BY name
) s on s.name = goal_scorer.name
JOIN squads on goal_scorer.name=squads.player
There are other ways to do it, but here I'm using a sub-query to get the total by player. NB: Most modern SQL platforms support windowing functions to do this too.
Also, probably don't need the left on the sub-query (since we know there will always be at least one name), but I put it in case your actual use case is more complicated.
Can you try this if you are using sql-server?
select *
from squads
outer apply(
selecr sum(goal_scorer.number) as score
from goal_scorer where goal_scorer.name=squads.player
)x

SQLite: How to return count of zero from left join?

I have two tables: players (id, name) and goals (id, player_id). I want to return a list of all players and the number of goals they have associated with them, even if they have no goals associated with them. I have tried:
SELECT player.name AS player_name,
COUNT(goal.id) AS goals
FROM player
LEFT JOIN goal
ON player.id = goal.player_id
GROUP BY player
ORDER BY goals DESC;
But the problem is that it only returns one player with zero goals, and I can't figure out why. I know it's something to do with the GROUP BY clause. There are definitely many players with zero goals.
You are grouping by the entire table, instead of single field in the player table that is unique (such as player.name perhaps).
You need to group by player.name. I'm surprised you're not getting an error that player is not a defined column.

SQL add columns of each record together

To be blunt I don't know SQL however I don't want the answer, I want to work it out myself.
Here's the question:
Write a SQL query to calculate the number of goals for each team.
players
id name team_id goals
1 Joel 1 3
2 Ed 2 1
3 Simon 2 4
teams
id name
1 New Zealand
2 London
What I'm asking for is an arrow to information that will allow me to solve the question.
I've tried looking myself but I don't even know the correct terminology to ask the question, googling 'write sql to add fields for each row' just seems to return about adding columns or inserting.
You need to first try to JOIN your tables(id in Teams will be linked to TeamId in Players.) based on the foreign key columns.
Then you need to do the GROUP BY and use the aggregate function SUM to get the goals for each team.
So your query will be like:
select t.name, sum(p.goals) as cnt,
from players p inner join teams t on p.teamid = t.id
group by t.name
First you have to group players by teams : use t1.id=t2.id to join values in the tables, and then group theme by "BROUP BY" t.name.
Then : user "SUM(value)" function who sum values .
select teams.name,sum(players.goals) from players,team where player.team_id=teams.id group by teams.name;

SQL Database SELECT question

Need some help with an homework assignment on SQL
Problem
Find out who (first name and last name) has played the most games in the chess tournament with an ID = 41
Background information
I got a table called Games, which contains information...
game ID
tournament ID
start_time
end_time
white_pieces_player_id
black_pieces_player_id
white_result
black_result
...about all the separate chess games that have taken place in three different tournaments ....
(tournaments having ID's of 41,42 and 47)
...and the first and last names of the players are stored in a table called People....
person ID (same ID which comes up in the table 'Games' as white_pieces_player_id and
black_pieces_player_id)
first_name
last_name
...how to make a SELECT statement in SQL that would give me the answer?
sounds like you need to limit by tournamentID in your where clause, join with the people table on white_pieces_player_id and black_pieces_player_id, and use the max function on the count of white_result = win union black_result = win.
interesting problem.
what do you have so far?
hmm... responding to your comment
SELECT isik.eesnimi
FROM partii JOIN isik ON partii.valge=isik.id
WHERE turniir='41'
group by isik.eesnimi
having count(*)>4
consider using the max() function instead of the having count(*)> number
you can add the last name to the select clause if you also add it to the group by clause
sry, I only speak American. What language is this code in?
I would aggregate a join to that table to a derived table like this:
SELECT a.last_name, a.first_name, CNT(b.gamecount) totalcount
FROM players a
JOIN (select cnt(*) gamecount, a.playerid
FROM games
WHERE a.tournamentid = 47
AND (white_player_id = a.playerid OR black_player_id = a.playerid)
GROUP BY playerid
) b
ON b.playerid = a.playerid
GROUP BY last_name, first_name
ORDER BY totalcount
something like this so that you are getting both counts for their black/white play and then joining and aggregating on that.
Then, if you only want the top one, just select the TOP 1