Questions tagged [top-n]
322 questions
2
votes
1 answer
Fast selection algorithm for duplicate-heavy inputs?
I've familiarized myself with quickselect and median-of-medians for fast selection of the k-th element in an unsorted array. If you try hard enough, you can guarantee the worst case time complexity to lie in O(n).
My problem is a little different.…

Captain Trojan
- 2,800
- 1
- 11
- 28
2
votes
4 answers
Top n in nested grouping using data.table
Goal: Grouped by quarter and name I want to have the top n names by count (see example below). So the desired output for top 1 (for the example below) would be:
2019 Q1 Klaus 2
2019 Q2 Karl 3
As this is just a toy example going forward I also…

rkraft
- 495
- 4
- 16
2
votes
2 answers
How to get the TOP N element corresponding to a condition with XPath
I have an XML like this
I'd like with one xpath (I'm in a c# context not an xslt template) get…

remi bourgarel
- 9,231
- 4
- 40
- 73
2
votes
3 answers
T-SQL: Need Top N to always return N rows, even if null or blank
T-SQL: Need Top N to always return N rows, even if null or blank
Typically, the command
Select Top 5 * FROM ourTable
will return up to 5 rows, but less, depending whether the rows exist.
I want to ensure that it always returns 5 rows, (or in…

JosephDoggie
- 1,514
- 4
- 27
- 57
2
votes
1 answer
TensorFlow metric: top-N accuracy
I'm using add_metric trying to create a custom metric that computes top 3 accuracy for a classifier. Here's as far as I got:
def custom_metrics(labels, predictions):
# labels => Tensor("fifo_queue_DequeueUpTo:422", shape=(?,), dtype=int64)
#…

rodrigo-silveira
- 12,607
- 11
- 69
- 123
2
votes
2 answers
Applying top and bottom N in one Power BI visualization
Charts and visuals on Power BI can be filtered to the top n records for easier at-a-glance reporting. However, if I want to report the top and bottom records (eg: top 10 and bottom 10 dollar amounts) I need to place two visuals. This consumes more…

PausePause
- 746
- 2
- 9
- 21
2
votes
3 answers
Multiply top h values times k for each row in a dataframe python
I have a dataframe with some dates as rows and values in columns. To have an idea the df looks like the below:
c1 c2 c3 c4
12/12/2016 38 10 1 8
12/11/2016 44 12 17 46
12/10/2016 13 6 2 7
12/09/2016 9 16 13 26
I…

clu
- 117
- 1
- 6
2
votes
2 answers
Return only rows with maximum key
I have two columns in my table:
- RECE_KEY
- INVE_KEY
I want to return only rows for INVE_KEY where RECE_KEY is max.
Example:
INVE_KEY = 1 is included in RECE_KEY = 1,2,3.
Max RECE_KEY in this example is 3, so correct result would…

FrenkyB
- 6,625
- 14
- 67
- 114
2
votes
1 answer
MySQL: Needing to return top 3 Users with the most votes. Results wanted in one column from the SUM of two subqueries. Java/Spring MVC
I have a Spring MVC blog with functionality for Post and Comment voting. I want to return the top 3 users based on number of votes they've received on all their posts and comments.
tables:
users u [id, username]
posts p [id, u.id]
comments c [id,…

RyanH
- 25
- 7
2
votes
4 answers
Get any value from a column aggregated by other column and where the max date is selected
I'm an SQL novice and I have a table with the following columns
+-------+--------+----+----+
| email | date | IP | ID |
+-------+--------+----+----+
And I want to do something like this:
SELECT T.email,
Max(T.date),
T.ip AS…

RustyRocketeer
- 21
- 3
2
votes
2 answers
About the longest time by max query
SELECT
D.DOG_ID, D.DOG_NAME, S.STORE_AREA, MAX(DURATION)
FROM
(SELECT
D.DOG_ID, D.DOG_NAME, S.STORE_AREA, SHD.START_TIME-END_TIME DURATION
FROM
SERVICE_HISTORY_DETAIL SHD, STORES S, DOGS D, SERVICE_HISTORY SH
…

Yvonne
- 53
- 1
- 5
2
votes
1 answer
How to do order by min() of some column?
I am pretty new to SQL. I want a query which should do order by on min of some column. Below is the query i want.
SELECT *
FROM (
SELECT p.PROJECT_ID,
p.PROJECT_NAME,
p.PROJECT_TYPE
FROM PROJECT…

viv kumar
- 252
- 4
- 13
2
votes
4 answers
Selecting top N rows for each group based on value in column
I have dataframe like below :-
x<-c(3,2,1,8,7,11,10,9,7,5,4)
y<-c("a","a","a", "b","b","c","c","c","c","c","c")
z<-c(2,2,2,1,1,3,3,3,3,3,3)
df<-data.frame(x,y,z)
df
x y z
1 3 a 2
2 2 a 2
3 1 a 2
4 8 b 1
5 7 b 1
6 11 c 3
7 10 c 3
8 …

vsb
- 428
- 6
- 15
2
votes
1 answer
Use Top n on category group in chart
I have a report showing a table showing details, it has about 380 rows.
I want to add a simple bar chart using the same dataset.
In the chart is one value and one group: value = sum(amount) and category group = description order by sum(amount).
This…

Peter
- 850
- 5
- 16
2
votes
1 answer
Find names of top-n highest-value (non-zero) columns in each pandas dataframe row
Suppose I have dataframe like
id p1 p2 p3 p4
1 0 9 0 4
2 0 0 0 4
3 1 3 10 7
4 1 5 3 1
5 2 3 7 10
Want to find column names of top-n highest-value columns in each pandas data frame row and want to…

Sushant Kulkarni
- 439
- 1
- 4
- 11