Questions tagged [top-n]

322 questions
0
votes
1 answer

Query for top N per group with pagination using MySQL

I've searched for some answers on SO but none comes up with what I want to achieve, so here comes another question about top N per group. DB Structure Currently I have a Post table as followed: post_id | post_title | post_text and a Comment table…
Khang Minh
  • 33
  • 1
  • 5
0
votes
0 answers

Extract N-th value as part of group or within a group

I have a large table (millions of records) that looks something like this. I need to get the top 30% (or 70th percentile) on daily basis. So the results should look something like the section table. The table already has the Bread_Date sorted in…
0
votes
2 answers

how to select one image of a object stored in another table

product table in SQL tables: Product: ID, Name ProductImage ID, ProductID, Image I want to select an image in select query of Product I need first/last image of product1, first/last image of product2, etc Something…
VISHMAY
  • 699
  • 5
  • 20
0
votes
0 answers

SQL Server select max(id) given other conditions in where clause

We have a database table with structure as such: id (PK - identity, integer) NPI (varchar) lastname (varchar) firstname (varchar) city (varchar) state (varchar) The nature of the table is that some NPIs are in the table twice or N times. We are…
Rob
  • 2,363
  • 7
  • 36
  • 54
0
votes
7 answers

SELECT most experienced person

I have to make a table of basketball players and a query which finds the player with the most experience I have tried SELECT firstName, lastName, MAX(experience) FROM Player but I'm assuming thats wrong. So basically I want to find the player with…
Bilal Haider
  • 99
  • 3
  • 5
  • 10
0
votes
3 answers

updating second to last record in oracle sql

I have 3 primary keys. CompanyName, EmpNo, StartDate. I want to create a stored procedure that can allow me to update only the second to last record of each empno. How can I achieve that? In this case I have to records per empnno but I want to…
Edu
  • 1
  • 2
0
votes
2 answers

showing the details of the student with the maximum marks in oracle 11g R2

goal I have a student table. I need to show the details viz. name of the student who has obtained the maximum marks in a test. definition of the tables the following is the code for the definition of the tables:(I have created this using the text to…
IcyFlame
  • 5,059
  • 21
  • 50
  • 74
0
votes
3 answers

SQL Server 2005 SELECT TOP 1 from VIEW returns LAST row

I have a view that may contain more than one row, looking like this: [rate] | [vendorID] 8374 1234 6523 4321 5234 9374 In a SPROC, I need to set a param equal to the value of the first column from the first row of the view.…
JoeBrockhaus
  • 2,745
  • 2
  • 40
  • 64
0
votes
4 answers

sql query on employ table

My employees table has 3 columns: emp_id, emp_sal, dept_id. I need to find, for each department, the employee(s) with the greatest salary. I need to return emp_id, emp_sal and dept_id for those employees. Thanks in advance
0
votes
2 answers

Oracle to retrieve maximum record

Table_A A_id 1 Tale_B B_id A_id 1 1 2 1 3 1 Table_C B_id Process_date 1 20130101 12:20:01 2 20130101 12:10:01 3 20130101 13:00:01 How to retrieve the maximum process_date from…
user2127414
  • 61
  • 1
  • 6
0
votes
2 answers

Rank function oracle sql

I am new to sql and working on assignment. There is a question, where I am supposed to use rank function, but I have no idea how to use that. I tried to find some information on the internet, but still having problems. I am posting question here, if…
Marshall Black
  • 87
  • 2
  • 3
  • 10
0
votes
1 answer

Oracle query to find the 2nd highest

Possible Duplicate: select 2nd row in Plsql I have this database movies (id: int, name: string, years: int, rating: float) movies_genres (movie_id: int, genre: string) I need to find the For each genre, find the 2nd best rated movie. Can this be…
user1495220
  • 111
  • 1
  • 2
  • 15
0
votes
2 answers

select 2nd row in Plsql

Lets say I have the following table: SomeTable( id, price ) How do I select the 2nd highest priced row from this table? Note : This has to be done in Pl/SQL, in a database agnostic way. Is it possible to do this without any loops? I…
Achow
  • 8,600
  • 6
  • 39
  • 49
0
votes
2 answers

display the cab with the highest overall maintenance cost

I'm not sure how to get the max of the sum. I thought i could just display it in descending order and then use "rownum=1" but that didnt work. Any suggestions? Here's my code. select ca_make, sum(ma_cost) from cab join maintain on ca_cabnum =…
Johnny Nguyen
  • 39
  • 1
  • 6
0
votes
1 answer

Get the top 10% of records based on score column

I have a SCORE column. How do I select the top 10% from EMPLOYEE table with top scores?
ozzboy
  • 2,672
  • 8
  • 42
  • 69