rank

SQL Server Query for Rank (RowNumber) and Groupings

百般思念 提交于 2019-11-27 14:30:41
问题 I have a table that has some columns: User, Category, Value And I want to make a query that will give me a ranking, of all the users by the value, but reset for the category. Example: user1 CategoryA 10 user2 CategoryA 11 user3 CategoryA 9 user4 CategoryB 3 user1 CategoryB 11 the query would return: Rank User Category 1 user2 CategoryA 2 user1 CategoryA 3 user3 CategoryA 1 user1 CategoryB 2 user4 CategoryB Any ideas? I write the query and specify the Category, It works but then I have to

Calculating percentile rank in MySQL

北战南征 提交于 2019-11-27 13:14:22
I have a very big table of measurement data in MySQL and I need to compute the percentile rank for each and every one of these values. Oracle appears to have a function called percent_rank but I can't find anything similar for MySQL. Sure I could just brute-force it in Python which I use anyways to populate the table but I suspect that would be quite inefficient because one sample might have 200.000 observations. This is a relatively ugly answer, and I feel guilty saying it. That said, it might help you with your issue. One way to determine the percentage would be to count all of the rows, and

Pandas rank by column value [duplicate]

本秂侑毒 提交于 2019-11-27 11:47:12
问题 This question already has an answer here: pandas group by year, rank by sales column, in a dataframe with duplicate data 1 answer I have a dataframe that has auction IDs and bid prices. The dataframe is sorted by auction id (ascending) and bid price (descending): Auction_ID Bid_Price 123 9 123 7 123 6 123 2 124 3 124 2 124 1 125 1 I'd like to add a column called 'Auction_Rank' that ranks auction id's by bid prices: Auction_ID Bid_Price Auction_Rank 123 9 1 123 7 2 123 6 3 123 2 4 124 3 1 124

Rank() over Partition by in mysql

自作多情 提交于 2019-11-27 09:39:23
I'm completely stumped as to create a new column "LoginRank" from rank() over(partition by x, order by y desc) in mysql. From sql server i would write the following query, to create a column "Loginrank" that is grouped by "login" and ordered by "id". select ds.id, ds.login, rank() over(partition by ds.login order by ds.id asc) as LoginRank from tablename.ds I have the following table. create table ds (id int(11), login int(11)) insert into ds (id, login) values (1,1), (2,1), (3,1), (4,2), (5,2), (6,6), (7,6), (8,1) I tried applying many existing mysql fixes to my dataset but continue to have

Unique Rank value for a subgroup within a group

落花浮王杯 提交于 2019-11-27 09:10:01
I am trying to get a unique rank value (e.g. {1, 2, 3, 4} from a subgroup in my data. SUMPRODUCT will produce ties{1, 1, 3, 4} , I am trying to add the COUNTIFS to the end to adjust the duplicate rank away. subgroup col B col M rank LMN 01 1 XYZ 02 XYZ 02 ABC 03 ABC 01 XYZ 01 LMN 02 3 ABC 01 LMN 03 4 LMN 03 4 'should be 5 ABC 02 XYZ 02 LMN 01 1 'should be 2 So far, I've come up with this. =SUMPRODUCT(($B$2:$B$38705=B2)*(M2>$M$2:$M$38705))+countifs(B2:B38705=B2,M2:M38705=M2) What have I done wrong here? The good news is that you can throw away the SUMPRODUCT function and replace it with a pair

Selecting top n elements of a group in Oracle

纵饮孤独 提交于 2019-11-27 06:29:36
问题 I have an Oracle table which has a name,value,time columns.Basically the table is for logging purposes to store what are the changes made to a particular name,what was the previous value and what time the change was made. I need to formulate a query to fetch the top n changes for a particular name,and the output should have all the names in the table. Any help/suggesstions? Edit: Name Value Time Harish Pass 1-Nov-2011 Ravi Fail 2-Nov-2011 Harish Absent 31-Oct-2011 Harish Attended 31-Aug-2011

Pandas Number Rows Within Group

拜拜、爱过 提交于 2019-11-27 04:26:39
问题 Given the following data frame: import pandas as pd import numpy as np df=pd.DataFrame({'A':['A','A','A','B','B','B'], 'B':['a','a','b','a','a','a'], }) df A B 0 A a 1 A a 2 A b 3 B a 4 B a 5 B a I'd like to create column 'C', which numbers the rows within each group in columns A and B like this: A B C 0 A a 1 1 A a 2 2 A b 1 3 B a 1 4 B a 2 5 B a 3 I've tried this so far: df['C']=df.groupby(['A','B'])['B'].transform('rank') ...but no dice! Thanks in advance! 回答1: Use groupby/cumcount: In [25

Update the rank in a MySQL Table

青春壹個敷衍的年華 提交于 2019-11-27 00:59:18
问题 I have the following table structure for a table Player Table Player { Long playerID; Long points; Long rank; } Assuming that the playerID and the points have valid values, can I update the rank for all the players based on the number of points in a single query? If two people have the same number of points, they should tie for the rank. UPDATE: I'm using hibernate using the query suggested as a native query. Hibernate does not like using variables, especially the ':'. Does anyone know of any

MySQL, Get users rank

有些话、适合烂在心里 提交于 2019-11-26 22:42:36
I have a mysql table like below: id name points 1 john 4635 3 tom 7364 4 bob 234 6 harry 9857 I basically want to get an individual user rank without selecting all of the users. I only want to select a single user by id and get the users rank which is determined by the number of points they have. For example, get back tom with the rank 2 selecting by the id 3. Cheers Eef SELECT uo.*, ( SELECT COUNT(*) FROM users ui WHERE (ui.points, ui.id) >= (uo.points, uo.id) ) AS rank FROM users uo WHERE id = @id Dense rank: SELECT uo.*, ( SELECT COUNT(DISTINCT ui.points) FROM users ui WHERE ui.points >= uo

Rank per row over multiple columns in R

£可爱£侵袭症+ 提交于 2019-11-26 20:59:39
问题 I'm using R for the analysis of my masterthesis. Unfortunately, I got stuck with this problem: I would like to compute a new variable which calculates the rank of one variable per row within many variables. Example: V1 V2 V3 NewVariable_V1 NewVariable_V2 NewVariable_V3 11 21 35 3 2 1 22 12 66 2 3 1 44 22 12 1 2 3 回答1: You're looking for rank. To get decreasing order, first negate the data.frame. data.frame(d, t(apply(-d, 1, rank, ties.method='min'))) # V1 V2 V3 V1.1 V2.1 V3.1 # 1 11 21 35 3 2