rank

MySQL Rank in the Case of Ties

空扰寡人 提交于 2019-12-01 14:47:05
I need some help dealing with ties when ranking in MySQL. For example: PLAYER | POINTS Mary: 90 Bob: 90 Jim: 65 Kevin: 12 Bob and Mary should both be ranked #1. Jim should be #3. Kevin should be #4. MySQL: SET @rank=0; SELECT @rank:=@rank +1 as rank, player, points FROM my_table How can I change the SELECT statement so that the ranking is correct in the case of ties? My real life problem is more complicated, but if I understand how to solve the above, then I should be set. Assuming name is unique SELECT t1.name, (SELECT COUNT(*) FROM table_1 t2 WHERE t2.score > t1.score) +1 AS rnk FROM table_1

MySQL Rank in the Case of Ties

旧街凉风 提交于 2019-12-01 13:23:28
问题 I need some help dealing with ties when ranking in MySQL. For example: PLAYER | POINTS Mary: 90 Bob: 90 Jim: 65 Kevin: 12 Bob and Mary should both be ranked #1. Jim should be #3. Kevin should be #4. MySQL: SET @rank=0; SELECT @rank:=@rank +1 as rank, player, points FROM my_table How can I change the SELECT statement so that the ranking is correct in the case of ties? My real life problem is more complicated, but if I understand how to solve the above, then I should be set. 回答1: Assuming name

Add a field and insert an increment value

北战南征 提交于 2019-12-01 13:00:49
I have this table : id ref data 1 111 data1 2 111 data2 3 111 data3 4 111 data4 5 222 data1 6 222 data2 7 222 data3 8 333 data1 9 333 data2 and I'd like to insert a new field, called for example order , where for each ref, I'll set a crescent value. So the output should be : id ref data order 1 111 data1 1 2 111 data2 2 3 111 data3 3 4 111 data4 4 5 222 data1 1 6 222 data2 2 7 222 data3 3 8 333 data1 1 9 333 data2 2 can I do this with a simple query? EDIT The example above is just an example. This is my real table on the database : CREATE TABLE `items` ( `id` int(11) unsigned NOT NULL auto

Add a field and insert an increment value

情到浓时终转凉″ 提交于 2019-12-01 11:22:01
问题 I have this table : id ref data 1 111 data1 2 111 data2 3 111 data3 4 111 data4 5 222 data1 6 222 data2 7 222 data3 8 333 data1 9 333 data2 and I'd like to insert a new field, called for example order , where for each ref, I'll set a crescent value. So the output should be : id ref data order 1 111 data1 1 2 111 data2 2 3 111 data3 3 4 111 data4 4 5 222 data1 1 6 222 data2 2 7 222 data3 3 8 333 data1 1 9 333 data2 2 can I do this with a simple query? EDIT The example above is just an example.

Sorting and ranking a dataframe by date and time in r

南笙酒味 提交于 2019-12-01 05:36:08
I have a dataframe as below. Originally it was just two columns/variables -"Timestamp" (which contains date and time) and "Actor". I broke down the "Timestamp" variable into "date" and "time" and then "time further down into "hours" and "mins". This then gives the following structure dataf<-structure(list(hours = structure(c(3L, 4L, 4L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 1L, 1L, 2L, 2L), .Label = c("9", "12", "14", "15", "16", "17"), class = "factor"), mins = structure(c(17L, 1L, 2L, 14L, 15L, 16L, 3L, 4L, 6L, 6L, 7L, 9L, 9L, 13L, 13L, 10L, 11L, 12L, 2L, 5L, 8L, 8L),

Getting rank of a row in mysql query

我们两清 提交于 2019-12-01 04:32:14
问题 I was using this query to assign rank to every name according to the votes they have got, but it returns with the error : 1248 - Every derived table must have its own alias Here is my code: SELECT @rownum:=@rownum+1 AS rank, name, vote FROM table, (SELECT @rownum:=0) ORDER BY vote DESC On modifying the query to this :- SELECT @rownum:=@rownum+1 AS rank, name, vote FROM table ORDER BY vote DESC I get as expected rank of the queries as NULL. Any help , how to get rank at first place ? NOTE: I

How to get a row rank?

烈酒焚心 提交于 2019-11-30 19:38:42
HI, I actually posted similar (or same?) question yesterday, but I thought I need to post a new question since I have short, but clear question. I have the following table. id point 1 30 2 30 3 29 4 27 5 28 6 26 what I want: get all the users order by rank. user #1 and #2 should have 1 as their rank value because they both have 30 points I want to query a rank by user id. I like to get 1 as the result of my rank when I query user #1 and #2 because both of them have 30 points Added on: 3/18 I tried Logan's query, but got the following result id point rank 1 30 1 2 30 1 3 29 3 4 27 5 5 28 4 6 26

Pandas rank by multiple columns

孤者浪人 提交于 2019-11-30 14:56:26
I am trying to rank a pandas data frame based on two columns. I can rank it based on one column, but how can to rank it based on two columns? 'SaleCount', then 'TotalRevenue'? import pandas as pd df = pd.DataFrame({'TotalRevenue':[300,9000,1000,750,500,2000,0,600,50,500], 'Date':['2016-12-02' for i in range(10)], 'SaleCount':[10,100,30,35,20,100,0,30,2,20], 'shops':['S3','S2','S1','S5','S4','S8','S6','S7','S9','S10']}) df['Rank'] = df.SaleCount.rank(method='dense',ascending = False).astype(int) #df['Rank'] = df.TotalRevenue.rank(method='dense',ascending = False).astype(int) df.sort_values([

Ranking by Group in MySQL

喜你入骨 提交于 2019-11-30 07:49:36
问题 I have a table with one column as follows: name ------- Michael Michael Michael Michael John John John Alex Alex I need to rank them to give: name | rank --------|------ Michael |1 Michael |2 Michael |3 Michael |4 John |1 John |2 John |3 Alex |1 Alex |2 How can I perform that? 回答1: There's nothing in mysql that lets you do this directly, but you can hack it in: SET @prev := null; SET @cnt := 1; SELECT name, IF(@prev <> name, @cnt := 1, @cnt := @cnt + 1) AS rank, @prev := name FROM yourtable

mysql: group by ID, get highest priority per each ID

别说谁变了你拦得住时间么 提交于 2019-11-30 07:17:27
问题 I have the following mysql table called "pics", with the following fields and sample data: id vehicle_id filename priority 1 45 a.jpg 4 2 45 b.jpg 1 3 56 f.jpg 4 4 67 cc.jpg 4 5 45 kt.jpg 3 6 67 gg.jpg 1 Is it possible, in a single query, to get one row for each vehicle_id, and the row be the highest priority? The result I'm looking for: array ( [0] => array( [id] => '2', [vehicle_id] => '45', [filename] => 'b.jpg', [priority] => '1' ), [1] => array( [id] => '3', [vehicle_id] => '56',