Get ranking on mysql produce wrong ranks - mysql

i'm trying to get ranking based on rating percentage so mysql query like
select c.id , sum((r.value * 20))/ count(r1.pagetypeid) as score, #curRank := #curRank + 1 AS rank from (SELECT #curRank := 0) cr, rating as r
inner join rateelement as r1 on r.elementid = r1.id
inner join ratesubscription as r2 on r.subscriptionid = r2.id
inner join consultant as c on r2.consultantid = c.id
where r1.displayorder not in (6) and r2.agencyid = 38
group by c.id order by score desc
but it returns wrong raking indexes
what's wrong with the query?

Ranking with variables often has issues with group by -- and even order by in the most recent versions of MySQL. So, use a subquery:
select x.*, (#curRank := #curRank + 1) AS rank
from (select c.id, sum((r.value * 20))/ count(r1.pagetypeid) as score
from rating r inner join
rateelement r1
on r.elementid = r1.id inner join
ratesubscription r2
on r.subscriptionid = r2.id inner join
consultant c
on r2.consultantid = c.id
where r1.displayorder not in (6) and r2.agencyid = 38
group by c.id
order by score desc
) x cross join
(SELECT #curRank := 0) cr;

Related

Assign a position value to row based on a SUM

I'm using the code below to get the total distance for teams that people have registered to and entered data, then assign a position to the team.
SELECT #curRow := #curRow + 1 AS position, ROUND(SUM(d.dist_activity_duration
* CASE
WHEN d.dist_is_distance = 0 THEN s.activity_steps / 2000
WHEN d.dist_is_distance = 1 THEN 1
END)
,2) AS miles, t.team_name AS team_name
FROM distance d
JOIN (SELECT #curRow := 0) r
JOIN activities a
ON a.id = d.dist_activity_id
JOIN steps s
ON s.id = a.steps_id
JOIN members AS m
ON d.member_id = m.id
JOIN teams AS t
ON t.id = m.member_team_id
GROUP BY team_name
ORDER BY miles DESC
The code above outputs the following results
position miles team_name
2 134.05 team 1
1 78.00 team 2
I would like position 1 to be assigned to the team with the highest miles, position 2 the 2nd highest team...and so on.
In MySQL 8+, you would just use row_number():
SELECT ROW_NUMBER() OVER (ORDER BY miles DESC) AS position, t.*
FROM (SELECT ROUND(SUM(d.dist_activity_duration *
CASE WHEN d.dist_is_distance = 0 THEN s.activity_steps / 2000
WHEN d.dist_is_distance = 1 THEN 1
END), 2) AS miles, t.team_name AS team_name
FROM distance d JOIN
activities a
ON a.id = d.dist_activity_id JOIN
steps s
ON s.id = a.steps_id JOIN
members m
ON d.member_id = m.id JOIN
teams t
ON t.id = m.member_team_id
GROUP BY team_name
) t
ORDER BY miles DESC;
Earlier versions of MySQL support variables but they do not play well with GROUP BY and ORDER BY. The solution is a subquery (as above):
SELECT (#rn := #rn + 1) AS position,
FROM (SELECT ROUND(SUM(d.dist_activity_duration *
CASE WHEN d.dist_is_distance = 0 THEN s.activity_steps / 2000
WHEN d.dist_is_distance = 1 THEN 1
END), 2) AS miles, t.team_name AS team_name
FROM distance d JOIN
activities a
ON a.id = d.dist_activity_id JOIN
steps s
ON s.id = a.steps_id JOIN
members m
ON d.member_id = m.id JOIN
teams t
ON t.id = m.member_team_id
GROUP BY team_name
ORDER BY miles DESC
) t CROSS JOIN
(SELECT #rn := 0) params;
This works for me.
SELECT (#rn := #rn + 1) AS position, team_name, miles
FROM (SELECT ROUND(SUM(d.dist_activity_duration
* CASE
WHEN d.dist_is_distance = 0 THEN s.activity_steps / 2000
WHEN d.dist_is_distance = 1 THEN 1
END)
,2) AS miles, t.team_name AS team_name
FROM distance d
JOIN activities a
ON a.id = d.dist_activity_id
JOIN steps s
ON s.id = a.steps_id
JOIN members AS m
ON d.member_id = m.id
JOIN teams AS t
ON t.id = m.member_team_id
GROUP BY team_name
ORDER BY miles DESC
) t CROSS JOIN
(SELECT #rn := 0) params;

SQL Rank Function with Group

I want to rank the total stats of a group of users and assign a rank variable to them.
I used this thread for the Rank variable.
This is my Query atm:
SELECT #rank := #rank + 1 AS rank
, SUM(stats.points) AS x
FROM
( SELECT #rank := 0 ) r
, groups
LEFT
JOIN user_group
ON groups.id = user_groups.clan
LEFT
JOIN stats
ON user_groups.user = stats.id
GROUP
BY groups.id
ORDER
BY x DESC
RANK | points
--------------
47 | 3400
1 | 2500
75 | 1200
As you can see the Sorting by Points works fine, but the Rank variable seems to just pick random values.
Can anyone find a way to assign the rank correctly?
Use a subquery for the aggregation and ordering:
SELECT id, sum_points, #rank := #rank + 1 AS rank
FROM (SELECT g.id, SUM(s.points) AS sum_points
FROM groups g LEFT JOIN
user_group ug
ON g.id = ug.clan LEFT JOIN
stats s
ON ug.user = s.id
GROUP BY g.id
ORDER BY sum_points DESC
) s CROSS JOIN
(SELECT #rank := 0) params;
This has been an issue in MySQL for a while -- variables don't work well with aggregation and ordering.
Note that in MySQL 8+, this is much more simply written as:
SELECT g.id, SUM(s.points) AS sum_points,
ROW_NUMBER() OVER (ORDER BY SUM(s.points) DESC) as rank
FROM groups g LEFT JOIN
user_group ug
ON g.id = ug.clan LEFT JOIN
stats s
ON ug.user = s.id
GROUP BY g.id

Rank function in MySQL assign a rank

I need to assign a rank to TC5 not the id.
SELECT a.id, a.user_id, a.tc1, a.tc4, min(a.tc5), a.tc2, b.avatar, c.username, #curRank := #curRank + 1 AS Rank
FROM
treningove_casy a INNER JOIN
sn_users b ON a.user_id=b.id INNER JOIN
users c ON a.user_id=c.id , (SELECT #curRank := 0) r
WHERE a.tc2 LIKE 'Motokáry Modrice'
GROUP BY a.user_id
So how is the picture. Please help
The modified code
SELECT x.*, (#curRank := #curRank + 1) as Rank
FROM (SELECT a.id, a.user_id, a.tc1, a.tc4, min(a.tc5) as tc5,
a.tc2, b.avatar, c.username,
FROM sbhgl_chronoengine_chronoforms_datatable_treningove_casy a INNER JOIN
sbhgl_jsn_users b
ON a.user_id = b.id INNER JOIN
sbhgl_users c
ON a.user_id = c.id
WHERE a.tc2 LIKE 'Motokáry Modřice'
GROUP BY a.user_id
) x CROSS JOIN
(SELECT #curRank := 0) params
ORDER BY tc5 DESC;
SELECT a.id, a.user_id, a.tc1, a.tc4, a.tc2, b.avatar, c.username,
(select rank from (SELECT
IF (#score=s.tc5, #rank:=#rank, #rank:=#rank+1) rank,
#score:=s.tc5 tc5s
FROM treningove_casy s,
(SELECT #score:=0, #rank:=0) r
ORDER BY tc5 DESC) s ) as rank
FROM
treningove_casy a INNER JOIN
sn_users b ON a.user_id=b.id INNER JOIN
users c ON a.user_id=c.id , (SELECT #curRank := 0) r
WHERE a.tc2 LIKE 'Motokáry Modrice'
GROUP BY a.user_id
You can try above solution, Hope this will help you.
In MySQL, you typically use variables for ranks. In your case, you would use a subquery. I find that the variable method doesn't always work with GROUP BY:
SELECT x.*, (#curRank := #curRank + 1) as Rank
FROM (SELECT a.id, a.user_id, a.tc1, a.tc4, min(a.tc5) as tc5,
a.tc2, b.avatar, c.username,
FROM treningove_casy a INNER JOIN
sn_users b
ON a.user_id = b.id INNER JOIN
users c
ON a.user_id = c.id
WHERE a.tc2 LIKE 'Motokáry Modrice'
GROUP BY a.user_id
) x CROSS JOIN
(SELECT #curRank := 0) params
ORDER BY tc5 DESC;
Note: The use of table aliases is good. It is much easier to understand a query, though, if the aliases are abbreviations for the table names.

Mysql how limit left join results

I have two tables: songs and groups
i want limit the songs are match the group to 3
i tried this:
SELECT
groups.`IDgroup`,
groups.`name` AS g_name,
songs.`IDsong`,
songs.`name` AS s_name
FROM `groups`
LEFT OUTER JOIN songs ON (groups.`IDgroup` = songs.`IDgroup` LIMIT 3)
Put the limit out of parentheses:
SELECT
groups.`IDgroup`,
groups.`name` AS g_name,
songs.`IDsong`,
songs.`name` AS s_name
FROM `groups`
LEFT OUTER JOIN songs
ON groups.`IDgroup` = songs.`IDgroup`
LIMIT 3
It is generally not a good idea to put a limit on a query that does not explicitly order its results. The reason is that it could return different results over time.
So, you may want to consider adding an
ORDER BY groups.IDgroup, songs.IDsong
to your query (before the LIMIT 3), assuming that this combination is unique.
SELECT
g.`IDgroup`,
g.`name` AS g_name,
s.`IDsong`,
s.`name` AS s_name
FROM `groups` g
LEFT OUTER JOIN songs s
using ('idgroup')
LIMIT 3
This query will return the last 3 songs for each group:
SELECT
c.`IDgroup`,
c.`name` AS g_name,
s.`IDsong`,
s.`name` AS s_name
FROM
groups c
JOIN (
SELECT
IF(#C != c.IDgroup, #ROWNUM := 1, #ROWNUM := #ROWNUM +1) AS RN,
#C := c.IDgroup,
c.IDgroup,
s.IDsong,
s.name
FROM groups c
LEFT JOIN songs s ON c.`IDgroup` = s.`IDgroup`
CROSS JOIN (SELECT #C := '') t2
ORDER BY c.IDgroup ASC
) s ON c.`IDgroup` = s.`IDgroup`
JOIN JOIN (
SELECT IDgroup, MAX(rn) AS mx
FROM (
SELECT
IF(#C != c.IDgroup, #ROWNUM := 1, #ROWNUM := #ROWNUM +1) AS rn,
#C := c.IDgroup,
c.IDgroup
FROM groups c
LEFT JOIN songs s ON c.`IDgroup` = s.`IDgroup`
CROSS JOIN (SELECT #C := '') t2
ORDER BY c.IDgroup ASC
) t
GROUP BY IDgroup
) maxsong ON maxsong.IDgroup = c.IDgroup AND s.rn BETWEEN maxsong.mx-2 AND maxsong.mx
ORDER BY c.IDgroup ASC, s.`name` ASC
Fiddle: http://sqlfiddle.com/#!2/b65c3b/1
Take the LIMIT out of the parentheses:
SELECT
groups.`IDgroup`,
groups.`name` AS g_name,
songs.`IDsong`,
songs.`name` AS s_name
FROM `groups`
LEFT OUTER JOIN songs USING (`IDgroup`)
LIMIT 3

MySQL - Rank COUNT from GROUP BY returning nil

I'm trying to follow this example: MySQL - ranking by count() and GROUP BY but my rank column keeps returning nil.
Here's my query:
SELECT
#rownum := #rownum+1 AS rank,
q.id,
q.Name,
q.count
FROM
(SELECT
Accounts.id,
Accounts.Name,
COUNT(Accounts.Name) AS count
FROM
player_to_team_histories
INNER JOIN
team_histories ON team_histories.id = player_to_team_histories.team_history_id
INNER JOIN
teams ON teams.id = team_histories.team_id
INNER JOIN
accounts ON accounts.id = teams.account_id
WHERE
accounts.AccountTypeId = 1 AND player_id IN (SELECT
player_id
FROM
player_to_team_histories
WHERE
player_to_team_histories.not_valid IS NULL AND team_history_id = (SELECT
team_history_id
FROM
player_to_team_histories
INNER JOIN
team_histories ON team_histories.id = player_to_team_histories.team_history_id
WHERE
player_to_team_histories.id = 574651))
GROUP BY Accounts.Name
ORDER BY count DESC)q
Every column except rank is returning as expected, and rank is returning null for every row.
You have to initialize your rownum to 0 before starting to increment it, either by
SET #rownum := 0;
before the query, or by a separate SELECT clause after your first FROM:
SELECT ...
FROM
(SELECT #rownum := 0 ) counter,
(SELECT
... )