Questions tagged [top-n]
322 questions
6
votes
4 answers
Top_n return both max and min value - R
is it possible for the top_n() command to return both max and min value at the same time?
Using the example from the reference page https://dplyr.tidyverse.org/reference/top_n.html
I tried the following
df <- data.frame(x = c(10, 4, 1, 6, 3, 1, 1))…

Yaahtzeck
- 217
- 2
- 13
6
votes
1 answer
Select top N with "for update skip locked" in Oracle
In Oracle, I can select the top 1 message in a sorted table with
select messageid from(
select
messageid,
RANK() over (order by messageid asc) as msg_rank
from messages
) where msg_rank=1;
And as I discovered in a…

Synesso
- 37,610
- 35
- 136
- 207
6
votes
1 answer
Pandas report top-n in group and pivot
I am trying to summarise a dataframe by grouping along a single dimension d1 and reporting summary statistics for each element of d1. In particular I am interested in the top n (index and values) for a number of metrics.
what I would like to…

seanv507
- 1,206
- 1
- 11
- 23
6
votes
2 answers
Sorting by a to z with Limit
I have one table customers which has one field 'name' with a to z names records.
I get records from a to z with asc query
SELECT * FROM `customers` ORDER BY name ASC
But how can i get 5 records which starts with all a to z alphabets with only one…

Tony Stark
- 8,064
- 8
- 44
- 63
5
votes
4 answers
SQL Query to Select the 'Next' record (similar to First or Top N)
I need to do a query to return the next (or prev) record if a certain record is not present. For instance consider the following table:
ID (primary key) value
1 John
3 Bob
9 Mike
10 …

user983043
- 111
- 1
- 2
- 13
5
votes
3 answers
Selecting top 10 counts in SQLite
I have a table which records questions, their answers, and their authors. The column names are as follows:
id, question, answers, author
I would like to get a list of top 10 authors who have written the most questions. So it would need to first…

null0pointer
- 1,533
- 3
- 15
- 29
5
votes
4 answers
Issues optimizing postgres search query
I am having an issue with the following PostgreSQL query it takes more than 10 seconds to run is there any way to speed this query up to a rational speed, I am simply looking for the most relevant search terms associated with videos on a very large…

user3354369
- 113
- 1
- 6
5
votes
1 answer
Average time complexity of finding top-k elements
Consider the task of finding the top-k elements in a set of N independent and identically distributed floating point values. By using a priority queue / heap, we can iterate once over all N elements and maintain a top-k set by the following…

bluenote10
- 23,414
- 14
- 122
- 178
5
votes
1 answer
Oracle and Pagination
In MySql, the concept of pagination can easily be implemented with a single SQL statement using the LIMIT clause something like the following.
SELECT country_id, country_name
FROM country c
ORDER BY country_id DESC
LIMIT 4, 5;
It would retrieve…

Tiny
- 27,221
- 105
- 339
- 599
5
votes
3 answers
Finding the most popular likes in my friend network
I am working on a way to find the most popular likes in my friend network. "Most popular in my friend network" is defined as "having the most number of likes by my friends".
Suppose each friend has an unique id and has a number of liked pages. So,…

Sourav
- 379
- 7
- 13
4
votes
9 answers
Find the second highest salary
Well it is a well known question. Consider the below
EmployeeID EmployeeName Department Salary
----------- --------------- --------------- ---------
1 T Cook Finance 40000.00
2 D Michael Finance…

user1025901
- 1,849
- 4
- 21
- 28
4
votes
6 answers
Efficiently find top-N values from multiple columns independently in Oracle
Suppose I have 30 billion rows with multiple columns, and I want to efficiently find the top N most-frequent values for each column independently, and with the most elegant SQL possible. For example, if I have
FirstName LastName FavoriteAnimal…

Joda
- 129
- 3
- 10
4
votes
3 answers
How to summarize the top 3 highest values in a dataset when there are ties
I have a data frame (my_data) and want to calculate the sum of only the 3 highest values even though there might be ties. I am quite new to R and I've used dplyr.
A tibble: 15 x 3
city month number
1 Lund jan…

Andrés Lagerlöf
- 83
- 9
4
votes
4 answers
Top N items from a Spark DataFrame/RDD
My requirement is to get the top N items from a dataframe.
I've this DataFrame:
val df = List(
("MA", "USA"),
("MA", "USA"),
("OH", "USA"),
("OH", "USA"),
("OH", "USA"),
("OH", "USA"),
("NY", "USA"),
…

Sam
- 358
- 2
- 3
- 15
4
votes
5 answers
In Java, I need to group the keys of a hash map based on the basis of their values
I need to find the topN keys.
I have an input as a HashMap in the form as (key : value):
Banana : 13
Apple: 12
Mango : 32
Orange : 12
Grape : 18
Pear : 12
Peach : 18
I created a linked HapMap that is sorted based on values:
private…

nirdesh80
- 47
- 1
- 3