ranking

Find the ranking of an integer in mysql [duplicate]

寵の児 提交于 2019-12-02 01:55:57
Possible Duplicate: Mysql rank function I have the following countryTable country clicks ------- ------ 0 222 66 34 175 1000 45 650 How do I get the ranking of say country 45 which is 2 in this case? Ordered by country ASC : SELECT 1+COUNT(*) AS ranking FROM countryTable WHERE country < 45 ; Ordered by clicks DESC : SELECT 1+COUNT(*) AS ranking FROM countryTable AS t JOIN countryTable AS c ON c.clicks > t.clicks WHERE t.country = 45 ; You can get 2 rank as below it like below: Select * from tabeName order by clicks limit 1,1 For 3 rank: Select * from tabeName order by clicks limit 2,1 SELECT *

Order unique values by frequency

天大地大妈咪最大 提交于 2019-12-02 01:02:00
问题 What function do I need to use to rank first four most popular words in list in R program? For example, c("apple", "banana", "apple", "banana", "banana", "desk", "pen", "pen", "pen", "pen") to make it like "pen" "banana" "apple" "desk" Thank you 回答1: You can sort the tabled values in decreasing order and then take the names to get the output you're looking for. Try this: > x <- c("apple", "banana", "apple", "banana", "banana", "desk", "pen", "pen", "pen", "pen") > names(sort(table(x),

Update a MySQL table with record rankings within groups

为君一笑 提交于 2019-12-02 00:21:16
I have a table called 'winners' with columns 'id', 'category', 'score', 'rank'. I need to update my table and asign a rank within the subcategories (category_id) which as per the sample is 2 but could be more than that in the future. Most anwers I've found are based around select statements which simply tends to just output the table view but I did find a very good 'Update' answer ( https://stackoverflow.com/a/2727239/4560380 ) specifically the answer update where ties are required to share the same rank. Sample CREATE TABLE winners ( id int, category_id int, score double, rank int ); INSERT

Order unique values by frequency

若如初见. 提交于 2019-12-01 20:43:53
What function do I need to use to rank first four most popular words in list in R program? For example, c("apple", "banana", "apple", "banana", "banana", "desk", "pen", "pen", "pen", "pen") to make it like "pen" "banana" "apple" "desk" Thank you You can sort the tabled values in decreasing order and then take the names to get the output you're looking for. Try this: > x <- c("apple", "banana", "apple", "banana", "banana", "desk", "pen", "pen", "pen", "pen") > names(sort(table(x), decreasing = TRUE)) ## [1] "pen" "banana" "apple" "desk" table is intuitive for base R. Here's a qdap approach:

MySQL query to dynamic “Ranking rows”

二次信任 提交于 2019-12-01 07:41:23
问题 I'm having problems running a query ranking. The inner SELECT gives the rows in order of ranking, for each line, the variable @rank increases, if not a position equal to the previous ranking. But the @rank is not really the correct position. I'm trying to do a ranking grouped and ordered by those with the highest value. SET @prev := NULL; SET @curr := NULL; SET @rank := 0; SELECT @prev := @curr, @curr := SUM( a.value ) AS SUM_VALUES, @rank := IF(@prev = @curr, @rank, @rank+1) AS rank, b.id AS

How should I handle “ranked x out of y” data in PostgreSQL?

左心房为你撑大大i 提交于 2019-12-01 05:10:45
I have a table that I would like to be able to present "ranked X out of Y" data for. In particular, I'd like to be able to present that data for an individual row in a relatively efficient way (i.e. without selecting every row in the table). The ranking itself is quite simple, it's a straight ORDER BY on a single column in the table. Postgres seems to present some unique challenges in this regard; AFAICT it doesn't have a RANK or ROW_NUMBER or equivalent function (at least in 8.3, which I'm stuck on for the moment). The canonical answer in the mailing list archives seems to be to create a

How should I handle “ranked x out of y” data in PostgreSQL?

元气小坏坏 提交于 2019-12-01 02:30:11
问题 I have a table that I would like to be able to present "ranked X out of Y" data for. In particular, I'd like to be able to present that data for an individual row in a relatively efficient way (i.e. without selecting every row in the table). The ranking itself is quite simple, it's a straight ORDER BY on a single column in the table. Postgres seems to present some unique challenges in this regard; AFAICT it doesn't have a RANK or ROW_NUMBER or equivalent function (at least in 8.3, which I'm

Simple MySQL Update Rank with Ties

假如想象 提交于 2019-12-01 00:02:24
I am attempting to store the rank of users based on a score, all it one table, and skipping ranks when there is a tie. For example: ID Score Rank 2 23 1 4 17 2 1 17 2 5 10 4 3 2 5 Each time a user's score is updated, The rank for the entire table must also be updated, so after a score update, the following query is run: SET @rank=0; UPDATE users SET rank= @rank:= (@rank+1) ORDER BY score DESC; But this doesn't support ties, or skipping rank numbers after ties, for that matter. I want to achieve this re-ranking in as few queries as possible and with no joins (as they seem rather time consuming)

Django ORM - Grouped aggregates with different select clauses

被刻印的时光 ゝ 提交于 2019-11-30 23:45:38
Imagine we have the Django ORM model Meetup with the following definition: class Meetup(models.Model): language = models.CharField() speaker = models.CharField() date = models.DateField(auto_now=True) I'd like to use a single query to fetch the language, speaker and date for the latest event for each language. >>> Meetup.objects.create(language='python', speaker='mike') <Meetup: Meetup object> >>> Meetup.objects.create(language='python', speaker='ryan') <Meetup: Meetup object> >>> Meetup.objects.create(language='node', speaker='noah') <Meetup: Meetup object> >>> Meetup.objects.create(language=

Django ORM - Grouped aggregates with different select clauses

会有一股神秘感。 提交于 2019-11-30 17:50:11
问题 Imagine we have the Django ORM model Meetup with the following definition: class Meetup(models.Model): language = models.CharField() speaker = models.CharField() date = models.DateField(auto_now=True) I'd like to use a single query to fetch the language, speaker and date for the latest event for each language. >>> Meetup.objects.create(language='python', speaker='mike') <Meetup: Meetup object> >>> Meetup.objects.create(language='python', speaker='ryan') <Meetup: Meetup object> >>> Meetup