I have two tables:
Fixture (id, homeId, awayId...)
FixtureStats(id,playerId, teamOwnerId, rating, goals)
I want to show both sum of goals for each players and team goals in which the player belongs to.
PlayerId TeamOwnerId Goals TeamGoals
1 2 0 9
2 2 1 9
3 2 3 9
4 2 5 9
5 3 0 12
....
The sql query below still lacks the teamgoals
SELECT TOP (100)
PERCENT fs.teamownerid,
fs.playerid,
Count(f.id) AS Apps,
Sum(fs.goal) AS Goals
FROM dbo.fixturestats AS fs
INNER JOIN dbo.fixture AS f ON fs.fixtureid = f.id
WHERE ( fs.rating > 0 )
GROUP BY fs.playerid, fs.teamownerid
Try this :
SELECT
X.playerid,
X.teamownerid,
X.goals,
( SELECT Sum(goal)
FROM fixturestats
WHERE X.teamownerid = teamownerid
) AS TeamGoals
FROM (
SELECT
TOP (100) PERCENT fs.teamownerid AS TeamOwnerId,
fs.playerid AS PlayerId,
Count(f.id) AS Apps,
Sum(fs.goal) AS Goals
FROM dbo.fixturestats AS fs
INNER JOIN dbo.fixture AS f ON fs.fixtureid = f.id
WHERE ( fs.rating > 0 )
GROUP BY fs.playerid,fs.teamownerid
) AS X
you can use Sum-Over for this :
select
*,
Sum(Goals) over (partition by TeamOwnerId)
from
FixtureStats
where
(Rating > 0)
Related
I have a table
or_id
emp_id
cs
val
100
1
x
3.4
100
1
x
4.5
100
1
y
5
100
1
y
6
200
2
a
12
200
2
b
11
200
2
c
14
I want my output table like:
or_id
emp_id
CS1
CS2
CS3
100
1
x
y
200
2
a
b
c
I tried every possible code but nothing seems to work. I want dynamic code for this.
This query is working, but for larger dataset the execution time is lengthy, so I need an optimized code.
select distinct or_id,emp_id,
(select cs from (
select distinct cost_center from orl where emp_id=m.emp_id) a limit 1 offset 0 ) cs1,
(select cost_center from (
select distinct cost_center from orl where emp_id=m.emp_id) a limit 1 offset 1 ) cs2,
(select cost_center from (
select distinct cost_center from orl where emp_id=m.emp_id) a limit 1 offset 2 ) cs3
from orl m
For three CS columns, in MYSQL8
WITH
sorted AS
(
SELECT
or_id,
emp_id,
cs,
ROW_NUMBER() OVER (PARTITION BY or_id, emp_id ORDER BY cs) AS ordinal
FROM
your_table
GROUP BY
or_id,
emp_id,
cs
)
SELECT
or_id,
emp_id,
MAX(CASE WHEN ordinal = 1 THEN cs END) AS cs1,
MAX(CASE WHEN ordinal = 2 THEN cs END) AS cs2,
MAX(CASE WHEN ordinal = 3 THEN cs END) AS cs3
FROM
sorted
GROUP BY
or_id,
emp_id
Demo: https://dbfiddle.uk/2QNiXt6O
I have 2 different tables in my database by the name of: rank, settings.
Here is how each table looks like with a few records in them:
Table #rank:
id points userid
-- ----- ------
1 500 1
2 300 2
3 900 3
4 1500 4
5 100 5
6 700 6
7 230 7
8 350 8
9 850 9
10 150 10
Table #settings:
userid active
------ ------
1 0
2 1
3 1
4 1
5 1
6 0
7 1
8 1
9 0
10 1
I want to get the rank of a specific user by user_id from the rank table ordering by their points. Also I would Only want to include the users in the ranking results, if they have active = 1 set in the settings table.
I have a simple ranking query, but it is not really effective, because it does include everyone even if the user is not active:
SELECT * FROM
(SELECT #sort:=#sort+1 AS sort, points, userid
FROM rank,
(SELECT #sort := 0) s
ORDER BY points DESC) t
WHERE userid= 8
Any idea, how could I achieve my goals here?
Few sub queries. First gets all the users who are active in the right order. That is used as a source for another query to add the rank. Then this is used as the source for the points and rank for the userid you are actually interested in
SELECT sort, points
FROM
(
SELECT #sort:=#sort + 1 AS sort, points, userid
FROM
(
SELECT rank.points, rank.userid
FROM rank
INNER JOIN settings
ON rank.userid = settings.userid
WHERE settings.active = 1
ORDER BY points DESC
) sub0
CROSS JOIN (SELECT #sort:=0) sub2
) sub1
WHERE sub1.userid = 8
Borrowing the idea from: https://stackoverflow.com/a/4474389/92063
SELECT
#rn:=#rn+1 AS RANK
,USER_ID
,POINTS
FROM (
SELECT
R.userid AS USER_ID
,R.points AS POINTS
FROM
rank R
INNER JOIN
settings S
ON R.userid = S.userid
WHERE
S.active = 1
ORDER BY
R.points DESC
) t1, (SELECT #rn:=0) t2;
I am trying to display a list of teams with the number of goals they have scored (and order them by greatest to smallest) but am having trouble with joining all the queries together
Table 1: Teams
teamid teamname
1 team1
2 team2
3 team3
Table 2: Results
id gameid teamid gf
1 1 1 5
2 2 1 3
3 1 2 0
4 2 2 2
5 3 3 0
What I'm trying to achieve
1. Team1 8
2. Team2 2
3. Team3 0
Get list of all teams
SELECT team.teamid, team.teamname
FROM teams team
Gets sum of goals for 1 team
SELECT COALESCE( SUM( gf ) , 0 ) goalsfor
FROM results
WHERE teamid = 1
Joining of queries
SELECT team.teamid,
team.teamname,
COALESCE(res.gf, 0) goalsfor
FROM teams team
LEFT JOIN
(SELECT COALESCE(SUM(res.gf), 0) goalsfor
FROM results res
GROUP BY teamid) res ON team.teamid = res.teamid
ORDER BY goalsfor DESC
Been stuck on joining the queries all day
SELECT teams.teamname, res.goals FROM teams JOIN (
SELECT COALESCE(SUM(results.gf),0) AS goals, results.teamid AS teamid FROM results
group by results.teamid) res
ON teams.teamid=res.teamid ORDER BY goals DESC;
My solution is:
SELECT t.id,
t.name,
SUM(r.gf) goalsfor
FROM team t
LEFT JOIN
results r ON t.id = r.teamId
GROUP BY t.id
ORDER BY goalsfor DESC
My result from my dummy table:
id name goalsfor
1 Apple 8
2 Banana 2
3 Carrot 0
I don't think you need COALESCE if you made your columns have a default of 0 and cannot be null.
Query:
SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,#row:=#row+1 rank
FROM mismatch_user u
LEFT JOIN ratingItems r ON u.user_id=r.uniqueName,
(SELECT #row:=0) pos
ORDER BY votes DESC,rating DESC
Output:
user_id votes rating rank
2 2 10 2
6 2 9 6
3 2 5 3
1 1 5 1
4 1 5 4
27 1 5 27
9 0 0 9
The ranking miserably not telling me the truth and it's basing on the user_id. Can anyone help me?
Does this help?
select r.*, #row:=#row+1 rank
from
(SELECT u.user_id,r.totalVotes votes,r.totalPoints rating
FROM mismatch_user u
LEFT JOIN ratingItems r ON u.user_id=r.uniqueName
) r
join (SELECT #row:=0) pos
ORDER BY r.votes DESC, r.rating DESC
You're generating a sequence and that can lead to tricky behavior.
If your query is correct, the safest way to sort by rank is now to embed it in another SELECT:
SELECT *
FROM (
SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,#row:=#row+1 rank
FROM mismatch_user u
LEFT JOIN ratingItems r ON u.user_id=r.uniqueName,
(SELECT #row:=0) pos
ORDER BY votes DESC,rating DESC) T
ORDER BY rank;
If my data is this
match homeTeam awayTeam homeTeamID awayTeamID homePoints awayPoints
1 Alpha Beta 1 2 4 2
2 Gamma Delta 3 4 6 0
3 Alpha Gamma 1 3 2 4
4 Delta Beta 4 2 3 3
I need to make a ladder for them but I can't get it just right
The results should look like this
Name played Points
Gamma 2 10
Alpha 2 6
Beta 2 5
Delta 2 3
So far my code looks like this
$query = "SELECT *
FROM (
SELECT homeTeam AS teamName,
COUNT(homeTeamID) AS matches_played,
SUM(if(homeTeamID, homePoints, 0)) AS total_points
FROM matches
UNION ALL SELECT awayTeam AS teamName,
COUNT(awayTeamID) AS matches_played,
SUM(if(awayTeamID, awayPoints, 0)) AS total_points
FROM matches
) all_points
GROUP BY teamName
ORDER BY total_points DESC ";
but all it did was show ALPHA played 4 games for 15 points and BETA played 4 games for 9 points - Gamma & Delta were gone :(
You do not have GROUP BY clause in inner query.
But it's still incorrect. try:
SELECT teamName, COUNT(*) AS played, SUM(points) as points
FROM (
SELECT homeTeam AS teamName,
homePoints AS points
FROM matches
UNION ALL SELECT awayTeam AS teamName,
awayPoints AS points
FROM matches
) all_points
GROUP BY teamName
ORDER BY total_points DESC