greatest-n-per-group

Select the SECOND LAST record in each group

≯℡__Kan透↙ 提交于 2019-11-29 20:08:32
问题 There is a table Remark that contains data as shown below: SerialNo | RemarkNo | Desp ============================================= 10 | 1 | rainy 10 | 2 | sunny 11 | 1 | sunny 11 | 2 | rainy 11 | 3 | cloudy 11 | 4 | sunny 12 | 1 | rainy What query will return the following result: 10 | 1 | rainy 11 | 3 | cloudy 12 | null | null That is, the second last record in each group should be returned? Assuming all the RemarkNo for a SerialNo are continuous. The larger the remark number, the later the

SQLAlchemy: show only latest result if a join returns multiple results

自作多情 提交于 2019-11-29 18:09:05
I'm trying to create a Flask app that shows the latest score from individual players. So a player can have multiple scores, but on the leaderboard I only want to show her most recent score. My models.py: class Player(db.Model): __tablename__ = 'player' id = db.Column(db.Integer, primary_key=True) firstname = db.Column(db.String, nullable=False) score = db.relationship('Score', backref='player', lazy='dynamic') def __init__(self, firstname): self.firstname = firstname def __repr__(self): return '<id {}>'.format(self.id) class Score(db.Model): __tablename__ = 'score' id = db.Column(db.Integer,

How do I select TOP 5 PERCENT from each group?

天涯浪子 提交于 2019-11-29 17:43:36
问题 I have a sample table like this: CREATE TABLE #TEMP(Category VARCHAR(100), Name VARCHAR(100)) INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP

How to select single row based on the max value in multiple rows [duplicate]

旧时模样 提交于 2019-11-29 17:01:40
问题 Possible Duplicate: SQL: Find the max record per group I have a table with four columns as such: name major minor revision p1 0 4 3 p1 1 0 0 p1 1 1 4 p2 1 1 1 p2 2 5 0 p3 3 4 4 This is basically ca table containing records for each version of a program. I want to do a select to get all of the programs and their latest version so the results would look like this: name major minor revision p1 1 1 4 p2 2 5 0 p3 3 4 4 I can't just group by the name and get the max of each column because then i

Select all threads and order by the latest one

ぐ巨炮叔叔 提交于 2019-11-29 16:56:03
Now that I got the Select all forums and get latest post too.. how? question answered, I am trying to write a query to select all threads in one particular forum and order them by the date of the latest post (column "updated_at"). This is my structure again: forums forum_threads forum_posts ---------- ------------- ----------- id id id parent_forum (NULLABLE) forum_id content name user_id thread_id description title user_id icon views updated_at created_at created_at updated_at last_post_id (NULLABLE) I tried writing this query, and it works.. but not as expected: It doesn't order the threads

SQL join to correlated subquery where tables are related by overlapping ranges

不想你离开。 提交于 2019-11-29 16:01:35
I have the following table structure: Item ID | Name -------- 1 | Apple 2 | Pear 3 | Banana 4 | Plum 5 | Tomato Event ItemStart | ItemEnd | EventType | EventDate -------------------------------------------- 1 | 2 | Planted | 2014-01-01 1 | 3 | Picked | 2014-01-02 3 | 5 | Eaten | 2014-01-05 The two tables are linked only by the primary key of Item and the range of ItemStart and ItemEnd (inclusive) in Event. Events always refer to contiguous sequences of Items, but not all the Events for a given Item will have the same range. Events never occur on the same date for a given Item. The query I'd

SQL Query to select bottom 2 from each category

与世无争的帅哥 提交于 2019-11-29 15:29:14
问题 In Mysql, I want to select the bottom 2 items from each category Category Value 1 1.3 1 4.8 1 3.7 1 1.6 2 9.5 2 9.9 2 9.2 2 10.3 3 4 3 8 3 16 Giving me: Category Value 1 1.3 1 1.6 2 9.5 2 9.2 3 4 3 8 Before I migrated from sqlite3 I had to first select a lowest from each category, then excluding anything that joined to that, I had to again select the lowest from each category. Then anything equal to that new lowest or less in a category won. This would also pick more than 2 in case of a tie,

MySQL huge tables JOIN makes database collapse

故事扮演 提交于 2019-11-29 15:28:44
Following my recent question Select information from last item and join to the total amount , I am having some memory problems while generation tables I have two tables sales1 and sales2 like this: id | dates | customer | sale With this table definition: CREATE TABLE sales ( id int auto_increment primary key, dates date, customer int, sale int ); sales1 and sales2 have the same definition, but sales2 has sale=-1 in every field. A customer can be in none, one or both tables. Both tables have around 300.000 records and much more fields than indicated here (around 50 fields). They are InnoDB. I

SQL: Filter rows with max value

雨燕双飞 提交于 2019-11-29 14:41:31
This is my table structure: File | Version | Function 1 | 1 | 1 1 | 2 | 1 1 | 3 | 1 1 | 2 | 2 2 | 1 | 4 3 | 2 | 5 I need it to return these rows only 1 | 3 | 1 2 | 1 | 4 3 | 2 | 5 Meaning I only want the functions that have the most recent version for each file. I do not want the result below, i.e unique function ids that are not the most recent version 1 | 3 | 1 1 | 2 | 2 ... I've looked at How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL? , but that returns the most recent unique function ids. The query needs to be sqlite3 compatible. An efficient way to do

Get first/last n records per group by

孤者浪人 提交于 2019-11-29 14:38:25
问题 I have two tables : tableA (idA, titleA) and tableB (idB, idA, textB) with a one to many relationship between them. For each row in tableA, I want to retrieve the last 5 rows corresponding in tableB (ordered by idB). I've tried SELECT * FROM tableA INNER JOIN tableB ON tableA.idA = tableB.idA LIMIT 5 but it's just limiting the global result of INNER JOIN whereas I want to limit the result for each different tableA.id How can I do that ? Thanks 回答1: I think this is what you need: SELECT tableA