top-n

SQL Query to Select the 'Next' record (similar to First or Top N)

本秂侑毒 提交于 2019-12-17 20:37:49
问题 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 Tom. I'd like to query a record that has id 7 or greater if 7 is not present. My questions are, Are these type of queries possible with SQL? What are such queries called in the DB world? Thanks! 回答1: Yes, it's possible, but implementation will depend on your RDBMS. Here's what it looks like in MySQL, PostgreSQL

Top n records per group sql in access

帅比萌擦擦* 提交于 2019-12-17 09:55:57
问题 I am making some software that tracks the scores of a test. There are multiple users, the details of which are stored in a user table. There is then a progress table which tracks a score with the date and the user who's score it is. I can already select the 3 most recent records for a chosen userID SELECT TOP 3 Progress.LoginID, Progress.Score, Progress.[Date Taken] FROM Progress WHERE (((Progress.LoginID)=[Enter LoginID:])) ORDER BY Progress.[Date Taken] DESC; And I can show all the records

Select top N with “for update skip locked” in Oracle

若如初见. 提交于 2019-12-14 03:45:19
问题 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 previous question I can select a row exclusively with select * from messages where rownum < 2 for update skip locked; However I can't merge these two concepts together select messageid from( select messageid, RANK() over (order by messageid asc) as msg_rank from messages ) where msg

Oracle to retrieve maximum record

南笙酒味 提交于 2019-12-13 20:08:18
问题 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 Table_C with references of Table_A A_id based on Table_C timing window.If i want to retrieve Table_C id and max(process_date) in timing window 20130101 12:09:00 to 12:21:00 then it should return id as 1 and process_date as 12:20:01 回答1: You can use a subquery that gets the max(process_date) : select c1.b_id, c2.MaxDate

MySQL top-N ranking and sum the rest of same group

社会主义新天地 提交于 2019-12-11 10:25:08
问题 I've researched most of the time with this topic, however I couldn't get a efficient and perfect answer regarding ranking (top 3) a MySQL table with group and aggregate using sum() to the rest. The data are as following: TS | Name | Count ============================= 1552286160 | Apple | 7 1552286160 | Orange | 8 1552286160 | Grape | 8 1552286160 | Pear | 9 1552286160 | Kiwi | 10 ... 1552286100 | Apple | 10 1552286100 | Orange | 12 1552286100 | Grape | 14 1552286100 | Pear | 16 1552286100 |

N Top Record Selection Based on Own SQL Statement in MS-Access

耗尽温柔 提交于 2019-12-11 08:48:32
问题 I'm re-writing a small ms-access application to take examinations on. What they want is for the tests to grab a set of random questions based on how large the exam's size is. If each exam was a set number of questions, I could just stick the number in the TOP statement and be done with it, but there are a variable number of questions for each exam, so I want to replace the constant number next to the TOP with a field from the query. What I basically want is like this: SELECT TOP tblExam.

Excluding only one MIN value on Oracle SQL

橙三吉。 提交于 2019-12-11 03:39:24
问题 I am trying to select all but the lowest value in a column (GameScore), but when there are two of this lowest value, my code excludes both (I know why it does this, I just don't know exactly how to correct it and include one of the two lowest values). The code looks something like this: SELECT Id, SUM(Score) / COUNT(Score) AS Score FROM (SELECT Id, Score FROM GameScore WHERE Game_No = 1 AND Score NOT IN (SELECT MIN(Score) FROM GameScore WHERE Game_No = 1 GROUP BY Id)) GROUP BY Id So if I am

For each dataframe row, get both the top-n values and the column-indices where they occur

依然范特西╮ 提交于 2019-12-10 22:46:06
问题 I have 1000x1000 matrix (of floating point numbers) as dataframe. Columns and rows are 0-1000. For each row, I want top-10 highest values and their index information. This turns out to be harder than I thought at first: for row, index in df.iterrows(): temp_row = row.copy() sort_row = temp_row.sort() # somehow I want indices as well It is also okay to find top-10 indices, if I can get the values later by some other method or direct indexing. 回答1: Method 1 Will give the output as same as the

Oracle and Pagination

淺唱寂寞╮ 提交于 2019-12-10 14:56:23
问题 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 the rows starting from 5 to 10 in the result set which the SQL query retrieves. In Oracle, the same thing can be achieved using row numbers with a subquery making the task somewhat tedious as follows. SELECT country_id, country_name FROM (SELECT rownum

Find names of top-n highest-value (non-zero) columns in each pandas dataframe row

浪子不回头ぞ 提交于 2019-12-09 21:02:30
问题 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 exclude zero value from top 3. id top1 top2 top3 1 p2 p4 2 p4 3 p3 p4 p2 4 p2 p3 p4/p1 5 p4 p3 p2 The present solutions return column names which are having zero too. Is there way to exclude zero values. have this solution arank = df.apply(np.argsort, axis = 1) ranked_cols = df.columns.to_series()