The database table contains goal actions.
type goal assist
goal Johnny James
goal Johnny James
goal James Bob
When using GROUP BY goal, assist it displays
player goals assists
Johnny 2 0
Johnny 0 0
James 1 0
James 0 2
Bob 0 0
Bob 0 1
but I need it to show players' goals and assists in one line. Cheers.
You can do it like this (although that might not be the fastest query, depending on the size of your database and indexes!):
SELECT players.player,
-- Use correlated subselects to count goals / assists
(SELECT COUNT(*) FROM actions WHERE goal = players.player) goals
(SELECT COUNT(*) FROM actions WHERE assist = players.player) assists
-- Get all distinct players (UNION = DISTINCT, here). Of course, if you
-- have an actual players table, use that one instead!
FROM (
SELECT goal player FROM actions UNION
SELECT assist FROM actions
) players
From your question, I'm not sure if type = goal is relevant for your query...
A possible solution would be to unpivot the player names first, then group with pivoting:
SELECT
Player,
COUNT(NULLIF(ScoreType, 'assist')) AS goals,
COUNT(NULLIF(ScoreType, 'goal')) AS assists
FROM (
SELECT
CASE s.ScoreType WHEN 'goal' THEN goal ELSE assist END AS Player,
s.ScoreType
FROM GoalActions
CROSS JOIN (
SELECT 'goal' AS ScoreType
UNION ALL SELECT 'assist'
) st
) s
GROUP BY
Player
Unpivoting is done using a cross join to a virtual table, and grouping/pivoting is implemented in the form of aggregating with CASE.
Related
I have some database I need to query.
It is necessary to write a query that finds out how many times the teams played among themselves. It is important if Team A plays against Team B and then Team B plays against Team A, it counts as the same event. That is, the result should be: A against B - 2 games.
SELECT
least(home_team, away_team) AS A
,greatest(home_team, away_team) AS B
,COUNT(*) AS games_count
FROM event_entity
GROUP BY A, B
HAVING COUNT(*) >= 1
ORDER BY A, B
I have this query.
But need this solve.
3 first rows in solving
Novara-Udinese 1
Brescia-Genoa 1
Fc Pacos de Ferreira - Trofense 1
This is my schema:
You already have the query that you need to build on the logic of getting the number of count of games. In order to display the event you can do as follows.
SELECT MAX(CONCAT(home_team,'-',away_team)) as game
,COUNT(*) AS games_count
FROM event_entity
GROUP BY least(home_team, away_team), greatest(home_team, away_team)
ORDER BY 1
Working on an exercise for school trying to calculate the number of points scored by a basketball player ONLY during their first game.
So if I have a table that reports lots of games (Separate rows for 1st and 2nd half) that looks like this:
Game Date Player Half Points
1990-01-01 Mike 1 10
1990-01-01 Mike 2 10
1990-01-03 Mike 1 5
1990-01-03 Ben 2 8
1990-01-05 Kelly 1 4
1990-01-05 Kelly 2 4
1990-01-07 Kelly 1 10
And I want it to end up like this:
Game Date Player Points
1990-01-01 Mike 20
1990-01-03 Ben 8
1990-01-05 Kelly 8
How would I do this?
I have been trying to use the code:
SELECT min(game_Date), player, sum(points);
But it keeps counting points for ALL games, not just points scored during the 1st game, of which there can be one record for the first half and one record for the second.
First you need to find the players' first games, like this
select player, min(game_date) as firstGameDate
from yourtable
group by player
and then get the points in that game by joining to the table again
select yourtable.player, firstgame.firstGameDate, sum(points) as firstGamePoints
from yourtable
inner join
(
select player, min(game_date) as firstGameDate
from yourtable
group by player
) firstgame
on yourtable.player = firstgame.player
and yourtable.game_date = firstgame.firstgameDate
group by yourtable.player, firstgame.firstgameDate
Some varieties of SQL allow you to use ranking functions which could eliminate the need to join to the table itself, but this will work in all varieties.
You have to use your logic. First you have to only grab the first game for each player (inner query). Then from there, you count the points
SELECT t.game_date, t.player, SUM(t.points)
FROM some_table t
JOIN (
SELECT player, MIN(game_date) AS min_date
FROM some_table
GROUP BY player
) a ON a.plyer = t.player AND a.min_date = t.game_date
GROUP BY t.player, t.game_date
Sub query approach of getting the result is given below
Filter the results only getting Player's first games.
Select min(Game_Date),Player from
basketball group by Player;
Use the results from first query to find each player's sum of scores in first game.
Select Game_Date, Player,sum(points) as first_play_points from
basketball where (Game_date,Player) in (Select min(Game_Date),Player
from basketball group by Player) group by Game_Date, Player;
Working fiddle can be found here
Sorry if my title isn't clear. I’m trying to make a lobby system for my card game. When a user presses join game I want the server to check how much space is available in the game they’ve selected then add them to the team with the least players (currently always 1 player on each team but I’d like to expand it to 2 players per team at some point and possibly more than 2 teams).
The tables I have are:
game_teams
game_id, team_id, score
game_players
user_id, game_id, team_id
With game_teams team_id isn't unique or auto-incremented because it seemed like more overhead to have a unique value for every team in every game when they're mostly just there for player placement. So all games have teams 1-2 or 1-3 depending on team count if that makes sense.
The output I’m hoping for is something like:
game_id | team_id | team_size
8 | 1 | 1
8 | 2 | 0
Currently the queries I have are as below but they don’t do what I’m expecting.
This returns all players in team 1 and all players in team 2 ignoring the game id
SELECT games.game_id, COUNT(game_players.user_id) AS team_size, game_teams2.team_id
FROM games
JOIN game_teams2 ON games.game_id=game_teams2.game_id
JOIN game_players ON game_players.team_id=game_teams2.team_id
WHERE games.game_id=1 GROUP BY game_teams2.team_id
This seems to be nearly right but it only returns teams that already have at least 1 player in them where I really need 0 to be returned if the team has no players associated with it.
SELECT game_players.game_id,
COUNT(game_players.user_id) AS team_size, game_players.team_id
FROM game_players WHERE game_players.game_id=8 GROUP BY game_players.team_id
I'm not quite sure what else to try to get the desired output.
Any help would be appreciated.
First, you need to create the distinct set of game_id and team_id from game_teams and then left join it with game_players.
Something like
SELECT x.game_id,
x.team_id,
count(gp.user_id) AS team_size
FROM
(SELECT DISTINCT game_id,
team_id
FROM game_teams
WHERE game_id = 8) x
LEFT JOIN game_players gp ON x.game_id = gp.game_id
AND x.team_id = gp.team_id
You need to take care of nulls for the gp.user_id. I have not tested this, this is just an idea.
How many (sum) goals were scored by each footballer in a given tournament in ascending order.
For those footballers with 0 goals, we should return name of the footballer with value 0. Point being, footballers with 0 goals should also be part of output.
Footballer name | Goal | Tournament
Messi | 3 | La liga
Ronaldo | 5 | UEFA
Suarez | 2 | La liga
Output should be for 'La Liga' output should be:
Ronaldo 0
Suarez 2
Messi 3
Try this
select footballer, sum(goals)
from (
select footballer, CASE
WHEN tournament = 'LA_LIGA' THEN goals
else 0
END
from tableA) AS tabA
group by footballer
Hope it help.
I'm not sure what's the structure of your database, I think normally the score is rewarded to a team instead of individuals. Anyway, here is just the concept:
Tables:
Players = {playerID, name, age, gender, height, etc..}
Games = {gameID, playerID, score...}
Tournaments = {tournamentID, gameID, tournamentName...}
select p.name, IFNULL(g.score,0), t.tournamentName
from players p
left join Games g on p.playerID = g.playerID
left join Tournaments t on g.GameID = t.GameID
where t.tournamentName = 'La liga'
Solution:
My point is you may need to use IFNULL(..) and set the default value to 0.
try this....
Select FootballerName,
sum(CASE WHEN tournament = 'LA LIGA' THEN Goal else 0 END) as Goal
from #Matches group by FootballerName
want to get all goals by players in all tournament
Select FootballerName, sum(Goal) as Goal
from #Matches group by FootballerName
want to know goals touranmentwise
Select FootballerName, sum(Goal) as Goal,Tournament
from #Matches group by FootballerName,Tournament
Source Of Information: Pivot query help us to generate an interactive table that quickly combines and compares large amounts of data. We can rotate its rows and columns to see different summaries of the source data, and we can display the details for areas of interest at a glance. It also help us to generate Multidimensional reporting.
Create Table script
Create table Matches(FootballerName varchar(100),Goal int ,Tournament varchar(100))
Sql Server Insert Records script
insert into Matches(FootballerName,Goal,Tournament)
select 'Messi ', 3 ,'La liga' union
select 'Ronaldo ', 5 ,'UEFA' union
select 'Surez ', 2 ,'La liga'
MySql Server script
Create table Matches(FootballerName varchar(100),Goal int ,Tournament varchar(100));
insert into Matches(FootballerName,Goal,Tournament)
select 'Messi ', 3 ,'La liga' union
select 'Ronaldo ', 5 ,'UEFA' union
select 'Surez ', 2 ,'La liga';
MYSQL Version
Select FootBallerName,
Sum(CASE WHEN Tournament= 'La liga' THEN goal ELSE 0 END) AS 'La liga',
Sum(CASE WHEN Tournament= 'UEFA' THEN goal ELSE 0 END) AS 'UEFA'from Matches
group by FootBallerName order by FootBallerName;
SQL Server Query using Pivot --- for Only [La liga]
SELECT FootballerName,isnull([La liga],0) as'La liga'
FROM (
SELECT
FootballerName ,isnull(goal,0) as'goal',
Tournament
FROM Matches
) as s
PIVOT
(
SUM(goal)
FOR Tournament IN ([La liga],[UEFA])
)AS pvt order by [La liga]
SQL Server Query using Pivot --- Only [La liga] and [UEFA]
SELECT FootballerName,isnull([La liga],0) as'La liga',isnull([UEFA],0) as'UEFA'
FROM (
SELECT
FootballerName ,isnull(goal,0) as'goal',
Tournament
FROM Matches
) as s
PIVOT
(
SUM(goal)
FOR Tournament IN ([La liga],[UEFA])
)AS pvt order by [La liga],[UEFA]
You can read more [https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx]
Unless you have 3 tables (players, tournaments, and goals) you'll need to create 3 sets using subselects and join them together. You need the cross-product of all tournaments and all footballers.
SELECT footballer_name, tournament FROM
(SELECT DISTINCT footballer_name FROM your_table) AS players
JOIN
(SELECT DISTINCT tournament FROM your_table) AS tournaments
The above should get you one record per unique pairing of footballer and tournament. See if you can get this working in your database and then finish it off with a final join for scores
I'm trying to accomplish the following-
I have 2 tables for soccer teams (not created by me, this is what I have to work with):
won_matches-
columns: team_id | match_name | scored_goals
lost_matches-
columns: team_id | match_name | scored_goals
teams_names-
team_id | team_name
(I don't care about the match name or the number of scored goals)
What I need to do is COUNT how many entries each team has in the won_matches table and how many entries it has in the lost_matches table, and then divide the number of lost_matches by the number of won_matches, thus getting a lost/won matches ratio.
I then need to present this ratio for each team (or all teams) along with its team name.
I tried somethings like this, but it doesn't work as needed:
SELECT b. team_name, (SELECT COUNT(team_id)
FROM won_matches [***optional; WHERE team_id=37***]) / COUNT(a.team_id)*100 AS lost_won_ratio
FROM lost_matches a
join teams_names b on a.team_id=b.team_id
[***optional; WHERE a.team_id=37***]
Would be grateful for your suggestions.
Something like this should work.
SELECT tn.teamID, sum(won_matches.teamID ) as WON, sum(lost_matches.teamID ) as LOST,(sum(won_matches.teamID )/sum(lost_matches.teamID )) as WLratio
From teams_names AS tn LEFT JOIN won_matches ON tn.teamID = won_matches.teamID LEFT JOIN lost_matches ON tn.teamID = lost_matches.teamID
Try something like this:
select team_id, Won, count(*) as Matches, sum(scored_goals) as Goals
from
(select 1 as Won, Team_id, scored_goals from won_matches
union all
select 0 as Won, team_id, scored_goals from lost_matches) x
group by team_id, Won
I think something like that will do the trick:
select team_id,
count(won), count(lost),
count(won)/(count(won)+count(lost)) as 'Win ratio' from
(
select True as won, NULL as lost, won_matches.* from won_matches
union all
select NULL as won, True as lost, lost_matches.* from lost_matches
) as S group by team_id
http://sqlfiddle.com/#!2/6dbaf/2 (EDIT: use a join to display team name)
Please notice I don't take into account possible drawn matches since I don't know how this is stored in your DB.
EDIT Notice as well I used count(won)/(count(won)+count(lost)) as count ratio formula. That seems more logical. If you stick with count(lost)/count(win) you will have to deal with the divide by 0 case...