I have this Query:
SET #row_num = 0;
SELECT
(SELECT #row_num := #row_num + 1) AS itempurchase_code,
(SELECT supplier_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY supplier_code ORDER BY COUNT(*) DESC LIMIT 1) AS supplier_code,
(SELECT user_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY user_code ORDER BY COUNT(*) DESC LIMIT 1) AS user_code,
22 AS status_code,
item_invoicecodesupplier AS item_invoicecodesupplier,
(SELECT itempurchase_date FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY itempurchase_date ORDER BY COUNT(*) DESC LIMIT 1) AS itempurchase_date
FROM qa_items_purchases b
GROUP BY (item_invoicecodesupplier)
ORDER BY itempurchase_code;
I get this Result:
If you look there is no (2) itempurchase_code column, What i can do to show the numbers in sequence?
You must add the row number in an outer query if your query contains a GROUP BY.
SET #row_num = 0;
SELECT (SELECT #row_num := #row_num + 1) AS itempurchase_code, *
FROM
(
SELECT ... -- your original query goes here
) AS T1
ORDER BY itempurchase_code
Related
I have this query:
SET #playerRank = 0;
SELECT *, #playerRank := #playerRank + 1 AS Rank FROM
( SELECT
P.name AS nome,
(playerMMR_RT(P.pid) - DecayTimeDiffRT(P.pid)) AS MMR,
P.pid
FROM
players_rt P
) AS TableData
ORDER BY
MMR DESC,
nome ASC
Please note that playerMMR_RT and DecayTimeDiffRT return two integer values but that's not relevant. The result is this:
But I want this output:
Basically the result is good because the rows are sorted by MMR. I'd like the rank to be like 1, 2, 3... but it seems that it is sorted with the rows as well.
How can I fix this? I need the table to be sorted and the rank to go 1, 2, 3... in the sorted table.
Try move the order by inside the subquery
SET #playerRank = 0;
SELECT *, #playerRank := #playerRank + 1 AS Rank
FROM (
SELECT
P.name AS nome,
(playerMMR_RT(P.pid) - DecayTimeDiffRT(P.pid)) AS MMR,
P.pid
FROM players_rt P
ORDER BY MMR DESC, nome ASC
) AS TableData
ORDER BY rank
Or try appply the rank at the ordered result
SET #playerRank = 0;
SELECT t.*, #playerRank := #playerRank + 1 AS Rank
from (
select *
FROM (
SELECT
P.name AS nome,
(playerMMR_RT(P.pid) - DecayTimeDiffRT(P.pid)) AS MMR,
P.pid
FROM players_rt P
) AS TableData
ORDER BY MMR DESC, nome ASC
) t
order by rank
I recently moved to a shared host that has MySQL 5.6.39 instead of MariaDB 10.x, I was wondering what would the equivalent of the following MariaDB statement in MySQL?
SELECT rank,
total
FROM
(SELECT ROW_NUMBER() OVER (
ORDER BY `prestige` DESC, `xp` DESC) AS rank,
(SELECT COUNT(*)
FROM Modular_LS) AS total,
steamid
FROM Modular_LS) sub
WHERE sub.steamid = '%s'
I got as far as this, but now I'm stuck
SELECT rank, total FROM
(SELECT #rank := #rank +1 as rank FROM Modular_LS,
(SELECT COUNT(*) FROM Modular_LS) AS total, steamid FROM Modular_LS) sub,
(SELECT #rank := 0) r ORDER BY `prestige` DESC, `xp` DESC) t;
The table structure contains the column steamid, xp, prestige
My goal is to order by prestige descending first and then xp descending to put it in a ranking like-order, then using WHERE query to find a specific player's ranking. The output of which contains the rank (position) and the total (total amount of records)
Maybe this will get you started:
SELECT #rank := IF(player_id = #prev, #rank + 1, 1), #prev := player_id
FROM ( SELECT #rank := 1, #prev = 0 ) AS init
JOIN ( SELECT player_id
FROM Modular_LS
ORDER BY prestige DESC, SP DESC
) AS x ;
After a few hours, this is what I came up with that solved my problem.
SELECT
sub.rank
,sub.total
FROM
(
SELECT
t.id
,t.steamid
,#rownum : = #rownum + 1 AS rank
,(
SELECT
COUNT (*)
FROM
Modular_LS
) AS total
FROM
Modular_LS t JOIN (
SELECT
#rownum : = 0
) r
ORDER BY
t.prestige DESC
,t.xp DESC
) sub
WHERE
sub.steamid = '%s'
Output: https://www.dropbox.com/s/q9bjrzndbzj0l2i/test3.PNG?dl=0
How do I incorporate a query into the current code if I want to get only the Position (row_number) = 3 if lets say the Stud_ID = 4?
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as Position, s.*
FROM
(
SELECT
Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
) AS s;
Thanks!!
Use a subselect over your query and filter it with your desired row number
SELECT *
FROM (
SELECT #row_num := #row_num + 1 AS `Position`, s.*
FROM
(
SELECT
Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
) AS s
CROSS JOIN (SELECT #row_num := 0) AS s1
) AS t
WHERE t.Position = 3
I have table with id (store user id) and score in different match. I want what is the position of a user.
So for i try this sql fiddle;
in this I am getting all the row but I need only user having id 3 and it position in the table.
like this:
Score Postion
26 3
Even i try to do like this but no success
MySql: Find row number of specific record
With MySQL, how can I generate a column containing the record index in a table?
I got the answer: http://sqlfiddle.com/#!2/b787a/2
select * from (
select T.*,(#rownum := #rownum + 1) as rownum from (
select sum(score) as S,id from mytable group by id order by S desc ) as T
JOIN (SELECT #rownum := 0) r
) as w where id = 3
Updated sqlfiddle and above query. Now it is working perfectly.
I think this should do the trick:
SELECT totalScore, rownum FROM (
SELECT id,sum(score) AS totalScore,(#rownum := #rownum + 1) AS rownum
FROM mytable
JOIN (SELECT #rownum := 0) r
group by id) result
WHERE result.ID = 3;
just add a where clause
select x.id,x.sum,x.rownum
from(
select id,sum(score) as sum,(#rownum := #rownum + 1) as rownum
from mytable
JOIN (SELECT #rownum := 0) r
group by id
) x
where id =3
I have a table
scores(user, score)
and I have this query
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as row_index, user, score
FROM scores ORDER BY score DESC
now I want to select, from this query result, the user with name 'john' and score '1400' to know what is his row_index, something like
SELECT row_index
FROM *result* WHERE user='john' AND score=1400
how do I do that? I tried
SET #row_num = 0;
SELECT row_index
FROM (SELECT #row_num := #row_num + 1 as row_index, user, score
FROM scores ORDER BY score DESC)
WHERE user='john' AND score=1400`
but phpMyAdmin says
#1248 - Every derived table must have its own alias
How can I do that?
Thank you,
Alessandro
You haven't added an alias to the derived table.
SELECT row_index FROM (...) AS alias
WHERE alias.user = 'john' AND alias.score = 1400
Query:
SET #row_num = 0;
SELECT a.row_index
FROM (SELECT #row_num := #row_num + 1 as row_index,
user,
score
FROM scores ) a
WHERE a.user='john' AND a.score=1400
ORDER BY a.score DESC
I think Ordering in Subquery not always allowed.
Try to use views.
SET #row_num = 0;
CREATE VIEW v AS SELECT #row_num := #row_num + 1 as row_index, user, score FROM scores ORDER BY score DESC;
SELECT row_index FROM v;
Try this:
SET #row_num = 0;
SELECT row_index FROM (SELECT #row_num := #row_num + 1 as row_index, user,
score FROM scores ORDER BY score DESC) AS alias1 WHERE user='john' AND score=1400