Join a table to itself to display average value? - mysql

I have a list of game scores for various teams and various years.
Database looks like this:
id |team|year|week|points
1 | Wildcats|2015|1|43
2 | Wildcats|2015|2|50
I want to create a display that shows each team in the database, its total points scored for the year, and the league average points for that year.
So it might look like:
Wildcats 2015 387 44.3
etc.
I was trying this, but it's not working:
SELECT g1.year, g1.team, xyz.total
FROM game g1
join (SELECT avg(points) as total, year
FROM game g2) xyz on g1.year=xyz.year
group by g1.year, g1.team

You can use a correlated subquery to get average points per year:
SELECT team, year, SUM(points) AS totalPoints,
(SELECT AVG(points)
FROM game AS g2
WHERE g2.year = g1.year) AS avgPoints
FROM game AS g1
GROUP BY team, year
Demo here
or, with joining a derived table:
SELECT g1.team, g1.year, SUM(g1.points) AS totalPoints,
g2.avgPoints
FROM game AS g1
JOIN (SELECT AVG(points) AS avgPoints, year
FROM game
GROUP BY year
) AS g2 ON g1.year = g2.year
GROUP BY team, year
Demo here

May this query is what you looking for:
select team,year,sum(points)as total,cast(avg(points)as decimal(10,1))
as average from your_table_name group by team,year

Related

Cross Play Query

I am trying to find the following variations. I have 10 games and individual ids associated to each game. I need to figure out the following:
distinct count of all who played all 10 games
same but for 9 games
etc
The closest formula I came up with:
SELECT distinct player_id
FROM ticket
WHERE game_code IN ('17','16') AND
create_date BETWEEN '2018-05-01'
AND '2018-05-31'
GROUP BY player_id
HAVING COUNT(*) = 2;

Grouping all in one tuple in SQL

I have a table EMP with employees id and their hireyear. And I have to get the amount of hired employees in lets say the the years 2002 and 2000. The output table should als contain the amount of hired employees in the whole time.
So the last is easy. I just have to write:
SELECT COUNT(id) AS GLOBELAMOUNT FROM EMP;
But how do I count the amount of hired employees in 2002?
I could write the following:
SELECT COUNT(id) AS HIREDIN2002 FROM EMP WHERE YEAR = 2002;
But how do I combine this in one tuple with the data above?
Maybe I should group the data by Hireyear first and then count it? But can not really imagine how I count the data for several years.
Hope u guys can help me.
Cheers,
Andrej
Use conditional aggregation, e.g.:
SELECT COUNT(id) AS GLOBELAMOUNT,
COUNT(CASE WHEN YEAR=2000 THEN 1 END) AS HIREDIN2000,
COUNT(CASE WHEN YEAR=2002 THEN 1 END) AS HIREDIN2002
FROM EMP;
In Microsoft SQL Server (Transact-SQL) at least, you can use a windowed aggregate function like this:
Select Distinct
Year
,count(Id) over (Partition by Year) as CountHiredInYear
,count(Id) over () as CountTotalHires
From EMP
This gives something like:
Year | CountHiredInYear | CountTotalHires
2005 | 3 | 12
2006 | 4 | 12
2007 | 5 | 12
Another SQL Server specific approach is the With Rollup keyword.
Select Year
,count(Id) as CountHires
From Emp
Group by Year
With Rollup
This adds a summary line for each level of grouping, with the total value for that set of rows. So here, you'd get an extra row where Year was NULL, with the value 12.
You could use two (or more) inline queries:
SELECT
(SELECT COUNT(id) FROM EMP) AS GLOBELAMOUNT,
(SELECT COUNT(id) FROM EMP WHERE YEAR = 2002) AS HIREDIN2002
or a CROSS JOIN:
SELECT GLOBELAMOUNT, HIREDIN2002
FROM
(SELECT COUNT(id) AS GLOBELAMOUNT FFROM EMP) g CROSS JOIN
(SELECT COUNT(id) AS HIREDIN2002 FROM EMP WHERE YEAR = 2002) h

Sum values across multiple tables

I'm having trouble trying to sum a field GROUPED BY a common user ID from TWO DIFFERENT tables.
To give you a little more info... I am trying to track player performances by date (i.e.: most points scored on ALL Nov. 14's). The database is split, one table for regular season games and one table for playoffs. So, for example, a player may have played a regular season game on May 3, 2001....but a playoff game on May 3, 2005.
So, I'm trying to build a view with the sums of every player on all dates.
What I have for a single table:
SELECT PlayerId,sum(Points) as TOT_PTS
FROM RS_games
WHERE DAY(Date)=$cur_day
AND MONTH(Date)=$cur_month
GROUP BY PlayerId
...but I can't figure how I could sum the values of each player across two tables without creating a third view as a "stepping stone".
Any ideas?
If you want the results by (DAY-MONTH) you can do:
SELECT playerID,
CONCAT (DAY(DATE), '-', MONTH(DATE)) AS DAY_MONTH,
SUM(points) AS Total_Points
FROM (
SELECT playerID, DATE, points
FROM rs_games
UNION ALL
SELECT playerID, DATE, points
FROM po_games
) a
GROUP BY 1, 2
sqlfiddle demo
This way, you would end up with a result with every playerId,dd-mm, and the sum of points that were scored in that specific day across the years.
Just to lay out what I was saying:
select
ALL_players.PlayerID as PlayerID
COALESCE(reg.Points, 0) + COALESCE(po.Points, 0) as Points
from
ALL_players
left join
(select PlayerID, DATE(Date) as Date, sum(Points) as Points
from RS_games
WHERE DAY(Date)=$cur_day AND MONTH(Date)=$cur_month
group by PlayerID) as reg
on reg.PlayerID = ALL_players.PlayerID
left join
(select PlayerID, DATE(Date) as Date, sum(Points) as Points
from PO_games group by DATE(Date), PlayerID
WHERE DAY(Date)=$cur_day AND MONTH(Date)=$cur_month
group by PlayerID) as po
on po.PlayerID = ALL_players.PlayerID
EDIT: Looking again at requirements this will need either a full outer join or some adjustment... adjusting (should be working now)

MySQL query problems with combined SUM

I have three tables here, that I'm trying to do a tricky combined query on.
Table 1(teams) has Teams in it:
id name
------------
150 LA Lakers
151 Boston Celtics
152 NY Knicks
Table 2(scores) has scores in it:
id teamid week score
---------------------------
1 150 5 75
2 151 5 95
3 152 5 112
Table 3(tickets) has tickets in it
id teamids week
---------------------
1 150,152,154 5
2 151,154,155 5
I have two queries that I'm trying to write
Rather than trying to sum these each time i query the tickets, I've added a weekly_score field to the ticket. The idea being, any time a new score is entered for the team, I could take that teams id, get all tickets that have that team / week combo, and update them all based on the sum of their team scores.
I've tried the following to get the results i'm looking for (before I try and update them):
SELECT t.id, t.teamids, (
SELECT SUM( s1.score )
FROM scores s1
WHERE s1.teamid
IN (
t.teamids
)
AND s1.week =11
) AS score
FROM tickets t
WHERE t.week =11
AND (t.teamids LIKE "150,%" OR t.teamids LIKE "%,150")
Not only is the query slow, but it also seems to not return the sum of the scores, it just returns the first score in the list.
Any help is greatly appreciated.
If you are going to match, you'll need to accommodate for the column only having one team id. Also, you'll need to LIKE in your SELECT sub query.
SELECT t.id, t.teamids, (
SELECT SUM( s1.score )
FROM scores s1
WHERE
(s1.teamid LIKE t.teamids
OR CONCAT("%,",s1.teamid, "%") LIKE t.teamids
OR CONCAT("%",s1.teamid, ",%") LIKE t.teamids
)
AND s1.week =11
) AS score
FROM tickets t
WHERE t.week =11
AND (t.teamids LIKE "150,%" OR t.teamids LIKE "%,150" OR t.teamids LIKE "150")
You don't need SUM function here ? The scores table already has it? And BTW, avoid subqueries, try the left join (or left outer join depending on your needs).
SELECT t.id, t.name, t1.score, t2.teamids
FROM teams t
LEFT JOIN scores t1 ON t.id = t1.teamid AND t1.week = 11
LEFT JOIN tickets t2 ON t2.week = 11
WHERE t2.week = 11 AND t2.teamids LIKE "%150%"
Not tested.
Well not the most elegant query ever, but it should word:
SELECT
tickets.id,
tickets.teamids,
sum(score)
FROM
tickets left join scores
on concat(',', tickets.teamids, ',') like concat('%,', scores.teamid, ',%')
WHERE tickets.week = 11 and concat(',', tickets.teamids, ',') like '%,150,%'
GROUP BY tickets.id, tickets.teamids
or also this:
SELECT
tickets.id,
tickets.teamids,
sum(score)
FROM
tickets left join scores
on FIND_IN_SET(scores.teamid, tickets.teamids)>0
WHERE tickets.week = 11 and FIND_IN_SET('150', tickets.teamids)>0
GROUP BY tickets.id, tickets.teamids
(see this question and the answers for more informations).

Mysql return matching users in subqueries

I'm looking for a way to write one query to compare the results of multiple mysql subqueries, and return users that are in each query.
I have a that contains fantasy football stats for players. To simplify, in this case there are 3 columns I'm using: player, points, year.
I'm looking to run a query that returns a list of players who finished in the top 50 (based on points) in both 2010 and 2011.
I've done lots of searching around on playing with subqueries, doing joins on one table, etc but am still coming up at a loss on how to approach this.
You can do something like this:
SELECT a.player
FROM (SELECT player FROM players WHERE Year = 2010 ORDER BY points DESC LIMIT 50) a
JOIN
(SELECT player FROM players WHERE Year = 2011 ORDER BY points DESC LIMIT 50) b
ON a.player = b.player
Here is an example. I assumed that you calculate top50 based on sum of points and you have several entries for each player in each year.
select y2010.player
from (
select player, sum from (
select st1.player player, sum(st1.points) sum from stats st1 where st1.year = 2010 group by st1.player order by sum desc
) t1 limit 50 offset 0
) y2010, (
select player, sum from (
select st1.player player, sum(st1.points) sum from stats st1 where st1.year = 2011 group by st1.player order by sum desc
) t1 limit 50 offset 0
) y2011
where y2010.player = y2011.player
You can use a UNION ALL, this will get you the Top 50 in both years and put them in the same result set, no joining required:
(
select player, year, points
from players
where year = 2010
order by points desc
limit 50
)
union all
(
select player, year, points
from players
where year = 2011
order by points desc
limit 50
);
It's slightly ambiguous whether you want:
all players who finished in the top 50 in 2010, as well as all players who finished in the top 50 in 2011:
SELECT *
FROM scores
WHERE year = 2010
AND points >= (SELECT MIN(points) FROM (
SELECT points
FROM scores
WHERE year = 2010
ORDER BY points DESC
LIMIT 50
) t)
UNION ALL
SELECT *
FROM scores
WHERE year = 2011
AND points >= (SELECT MIN(points) FROM (
SELECT points
FROM scores
WHERE year = 2011
ORDER BY points DESC
LIMIT 50
) t)
all players who finished in the top 50 in both 2010 and 2011, in which case you'll need to further group the results:
SELECT player
FROM (
-- query as above
) t
GROUP BY player
HAVING COUNT(DISTINCT year) = 2