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
Related
I have the table with data:
And for this table I need to create pegination by productId column. I know about LIMIT N,M, but it works with rows and not with groups. For examle for my table with pegination = 2 I expect to retrieve all 9 records with productId = 1 and 2 (the number of groups is 2).
So how to create pegination by numbers of groups ?
I will be very thankfull for answers with example.
One way to do pagination by groups is to assign a product sequence to the query. Using variables, this requires a subquery:
select t.*
from (select t.*,
(#rn := if(#p = productid, #rn + 1,
if(#rn := productid, 1, 1)
)
) as rn
from table t cross join
(select #rn := 0, #p := -1) vars
order by t.productid
) t
where rn between X and Y;
With an index on t(productid), you can also do this with a subquery. The condition can then go in a having clause:
select t.*,
(select count(distinct productid)
from t t2
where t2.productid <= t.productid)
) as pno
from t
having pno between X and Y;
Try this:
select * from
(select * from <your table> where <your condition> group by <with your group>)
LIMIT number;
**castID**
nm0000116
nm0000116
nm0000116
nm0000116
nm0000116
nm0634240
nm0634240
nm0798899
This is my table (created as a view). Now I want to list the castID which has the most count (in this case which is nm0000116, and how many occurences/count it has in this table ( should be 5 times) and I'm not quite sure which query to use
try
Select CastId, count(*) countOfCastId
From table
Group By CastId
Having count(*)
= (Select Max(cnt)
From (Select count(*) cnt
From table
Group By CastId) z)
SELECT
MAX(E),
castId
FROM
(SELECT COUNT(castId) AS E,castId FROM [directors winning movies list] GROUP BY castId) AS t
You could just return the topmost count using LIMIT:
SELECT castID,
COUNT(*) AS Cnt
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
LIMIT 1
;
However, if there can be ties, the above query would return only one row. If you want all the "winners", you could take the count from the above query as a scalar result and compare it against all the counts to return only those that match:
SELECT castID,
COUNT(*) AS Cnt
FROM atable
GROUP BY castID
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
LIMIT 1
)
;
(Basically, same as Charles Bretana's approach, it just derives the top count differently.)
Alternatively, you could use a variable to rank all the counts and then return only those that have the ranking of 1:
SELECT castID,
Cnt
FROM (
SELECT castID,
COUNT(*) AS Cnt,
#r := IFNULL(#r, 0) + 1 AS r
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
) AS s
WHERE r = 1
;
Please note that with the above method the variable must either not exist or be pre-initialised with a 0 or NULL prior to running the query. To be on the safe side, you could initialise the variable directly in your query:
SELECT s.castID,
s.Cnt
FROM (SELECT #r := 0) AS x
CROSS JOIN
(
SELECT castID,
COUNT(*) AS Cnt,
#r := #r + 1 AS r
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
) AS s
WHERE s.r = 1
;
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 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
I've been wrestling with this query for a while. Here it is:
select First10.mal, Last10.family_name,
(select * from gender2
JOIN
(select * from status) as Mix1
JOIN
(select * from age as Mix2 order by rand() limit 10) as Mix3
JOIN
(select incidentid from incidentid2 as Mix4)
as Mix5 where data='mal' and incidentid='6' and status IN ('inj','ali') and age IN ('NULL','0-17')
order by rand() limit 100)
from ( select fn.mal, #fns := #fns + 1 as Sequence
from ( select mal from fnames where mal IS NOT NULL order by rand() limit 100) fn,
(select #fns := 0 ) vars ) First10
JOIN
( select ln.family_name, #lns := #lns + 1 as Sequence
from ( select family_name from lastnames order by rand() limit 100 ) ln,
(select #lns := 0 ) vars ) Last10
ON First10.Sequence = Last10.Sequence;
My goal here is to insert into a table which would present a randomized first name, last name, gender, status, age, and incident id. I have tried many ways to rework this script, including seperating the select statements, but I always seem to end up with this error:
ERROR 1241 (21000): Operand should contain 1 column(s)
Please advise guys, this has been stressing me out for a while now... It's probably a very simple answer, but I am just confusing myself further. If you need any clarifications, just ask.
Well, it looks like after a good night's sleep and some help from a friend, I got this query working. For those that are looking for an answer to a similar question, here is how I got this to work:
select mal,family_name,data,age,status,incidentid
from ( select fn.mal, #fns := #fns + 1 as Sequence
from ( select mal from fnames where mal IS NOT NULL order by rand() limit 100) fn,
(select #fns := 0 ) vars ) as FN
INNER JOIN
(select ln.family_name, #lns := #lns + 1 as Sequence
from ( select family_name from lastnames order by rand() limit 100 ) ln,
(select #lns := 0 ) vars ) as LN
INNER JOIN
(select * from gender2) as Mix0
INNER JOIN
(select * from status) as Mix1
INNER JOIN
(select * from age as Mix2 order by rand() limit 3) as Mix3
INNER JOIN
(select incidentid from incidentid2 as Mix4)
as Mix5 where data='mal' and incidentid='6' and status IN ('inj','ali') and age IN ('NULL','0-17')
order by rand() limit 100;