rank

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

我与影子孤独终老i 提交于 2019-11-29 02:37:40
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', [filename] => 'f.jpg', [priority] => '4' ), [2] => array( [id] => '6', [vehicle_id] => '67', [filename] => 'gg

Get top 10 products for every category

不问归期 提交于 2019-11-29 02:36:37
I have a query which is something like this SELECT t.category, tc.product, tc.sub-product, count(*) as sales FROM tg t, ttc tc WHERE t.value = tc.value GROUP BY t.category, tc.product, tc.sub-product; Now in my query I want to get top 10 products for every category (top by sales ) and for every category I need top 5 sub category (top by sales) You can assume the problem statement as something like this : Get top 10 products for each category by sales and for each product get top 5 sub-products by sales . Here category can be Books Product can be Harry Porter book sub productcan be HarryPorter

What does the := operator mean in mysql?

十年热恋 提交于 2019-11-29 02:24:22
问题 I have a mysql table ( scho_id , school_name , school_views ). I was looking for a mysql query to get rank of schools on the basis of school_views . I found this solution on stackoverflow. SET @points := -1, @num := 0; SELECT scho_id , school_views , @num := if(@points = school_views, @num, @num + 1) as school_rank , @points := school_info.school_views as dummy FROM school_info ORDER BY school_views desc, scho_id asc; This solved my problem but I notice a new operator := in this query. I am

How to partition when ranking on a particular column?

最后都变了- 提交于 2019-11-29 00:39:59
问题 All: I have a data frame like the follow.I know I can do a global rank order like this: dt <- data.frame( ID = c('A1','A2','A4','A2','A1','A4','A3','A2','A1','A3'), Value = c(4,3,1,3,4,6,6,1,8,4) ); > dt ID Value 1 A1 4 2 A2 3 3 A4 1 4 A2 3 5 A1 4 6 A4 6 7 A3 6 8 A2 1 9 A1 8 10 A3 4 dt$Order <- rank(dt$Value,ties.method= "first") > dt ID Value Order 1 A1 4 5 2 A2 3 3 3 A4 1 1 4 A2 3 4 5 A1 4 6 6 A4 6 8 7 A3 6 9 8 A2 1 2 9 A1 8 10 10 A3 4 7 But how can I set a rank order for a particular ID

SQL Server Query for Rank (RowNumber) and Groupings

喜欢而已 提交于 2019-11-28 23:06:55
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 write loops and its very slow. Use "Partition by" in the ranking function OVER clause SELECT Rank() over

Pandas rank by column value [duplicate]

六眼飞鱼酱① 提交于 2019-11-28 19:22:04
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 2 2 124 1 3 125 1 1 Zero Here's one way to do it in Pandas-way You could groupby on Auction_ID and take

Selecting top n elements of a group in Oracle

怎甘沉沦 提交于 2019-11-28 11:46:33
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 Harish Present 31-Jul-2011 I need to select details of Harish on 1st Nov,Oct 31st,31st Aug and Ravi.

Set array's rank at runtime

二次信任 提交于 2019-11-28 11:45:51
I have written a program which reads a file containing multidimensional data (most commonly 3D, but 2D could occur as well). To heighten simplicity I would like to store the data in an array of the same rank (or something pretending to be one), i.e. using a three-dimensional array for 3D data, etc.; the trouble is that the program only learns about the dimensionality on reading the data file. Currently I store all data in an array of rank one and calculate each element's index in that array from the element's coordinates (this was also suggested here ). However, I have also read about pointer

MySQL Help: Optimize Update Query that Sets Rank According to Order of Another Column

牧云@^-^@ 提交于 2019-11-28 10:06:52
问题 Hello StackOverflow community: The Situation I am building an update query that sets ranks for all records in a table according to how each record compares to each other. Example of table: id | budget | cost | rank | rank_score 1 | 500 | 20 | ? | ? 2 | 400 | 40 | ? | ? 3 | 300 | 40 | ? | ? So in this table, cost have the most weight in determining rank, followed by budget . Thus, record #2 will rank higher, while record #3 will be second and #1 will be last. As you can see, if two records

rank data over a rolling window in pandas DataFrame

不羁的心 提交于 2019-11-28 08:37:15
问题 I am new to Python and the Pandas library, so apologies if this is a trivial question. I am trying to rank a Timeseries over a rolling window of N days. I know there is a rank function but this function ranks the data over the entire timeseries. I don't seem to be able to find a rolling rank function. Here is an example of what I am trying to do: A 01-01-2013 100 02-01-2013 85 03-01-2013 110 04-01-2013 60 05-01-2013 20 06-01-2013 40 If I wanted to rank the data over a rolling window of 3 days