limit-per-group

How to select last 5 results from each user_id in an sqlite database?

丶灬走出姿态 提交于 2020-08-12 01:55:07
问题 I have a database with "num" table like this user_id | number | unix_time ----------------------------- 123 2 xxxxxxxx 123 40 xxxxxxxx 123 24 xxxxxxxx 333 23 xxxxxxxx 333 67 xxxxxxxx 854 90 xxxxxxxx I'd like to select the last 5 numbers inserted by each user_id, but I can't figure out how to do it. I tried: SELECT b.n, a.user_id FROM num a JOIN num b on a.user_id = b.user_id WHERE ( SELECT COUNT(*) FROM num b2 WHERE b2.n <= b.n AND b2.user_id = b.user_id ) <= 5 回答1: I am adapting the answer

SQLite: return only top 2 results within each group

眉间皱痕 提交于 2020-01-02 04:25:35
问题 I checked other solutions to similar problems, but sqlite does not support row_number() and rank() functions or there are no examples which involve joining multiple tables, grouping them by multiple columns and returning only top N results for each group at the same time. Here's the code i run db = sqlite3.connect('mydb') cursor = db.cursor() cursor.execute( ''' CREATE TABLE orders( id INTEGER PRIMARY KEY, product_id INTEGER, client_id INTEGER ) ''' ) cursor.execute( ''' CREATE TABLE clients(

How to select the first N rows of each group?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 02:45:14
问题 I have two SQLite tables like this: AuthorId | AuthorName ---------------------- 1 | Alice 2 | Bob 3 | Carol ... | .... BookId | AuthorId | Title ---------------------------------- 1 | 1 | aaa1 2 | 1 | aaa2 3 | 1 | aaa3 4 | 2 | ddd1 5 | 2 | ddd2 ... | ... | ... 19 | 3 | fff1 20 | 3 | fff2 21 | 3 | fff3 22 | 3 | fff4 I want to make a SELECT query that will return the first N (e.g. two) rows for each AuthorId, ordering by Title ("Select the first two books of each author"). Sample output:

Laravel: hasMany() with take() only working on first result

流过昼夜 提交于 2019-12-13 00:44:52
问题 I have the following models. class User extends Eloquent { public function comments() { return $this->hasMany('Comment'); } } class Comment extends Eloquent { public function user() { return $this->belongsTo('User'); } } For the sake of this example, a user could have 1,000s of comments. I am trying to limit them to just the first 10. I have tried doing it in the User model via class User extends Eloquent { public function comments() { return $this->hasMany('Comment')->take(10); } } and via

SQLite: return only top 2 results within each group

时光毁灭记忆、已成空白 提交于 2019-12-05 09:42:05
I checked other solutions to similar problems, but sqlite does not support row_number() and rank() functions or there are no examples which involve joining multiple tables, grouping them by multiple columns and returning only top N results for each group at the same time. Here's the code i run db = sqlite3.connect('mydb') cursor = db.cursor() cursor.execute( ''' CREATE TABLE orders( id INTEGER PRIMARY KEY, product_id INTEGER, client_id INTEGER ) ''' ) cursor.execute( ''' CREATE TABLE clients( id INTEGER PRIMARY KEY, gender TEXT, city TEXT ) ''' ) cursor.execute( ''' CREATE TABLE products( id

Average of latest N records per group

天大地大妈咪最大 提交于 2019-11-30 08:17:29
问题 My current application calculates a point average based on all records for each user: SELECT `user_id`, AVG(`points`) AS pts FROM `players` WHERE `points` != 0 GROUP BY `user_id` The business requirement has changed and I need to calculate the average based on the last 30 records for each user. The relevant tables have the following structure: table: players; columns: player_id, user_id, match_id, points table: users; columns: user_id The following query does not work, but it does demonstrate

Average of latest N records per group

流过昼夜 提交于 2019-11-29 06:35:36
My current application calculates a point average based on all records for each user: SELECT `user_id`, AVG(`points`) AS pts FROM `players` WHERE `points` != 0 GROUP BY `user_id` The business requirement has changed and I need to calculate the average based on the last 30 records for each user. The relevant tables have the following structure: table: players; columns: player_id, user_id, match_id, points table: users; columns: user_id The following query does not work, but it does demonstrate the logic that I am trying to implement. SELECT @user_id := u.`id`, ( -- Calculate the average for

How to select the first N rows of each group?

百般思念 提交于 2019-11-27 07:37:19
I have two SQLite tables like this: AuthorId | AuthorName ---------------------- 1 | Alice 2 | Bob 3 | Carol ... | .... BookId | AuthorId | Title ---------------------------------- 1 | 1 | aaa1 2 | 1 | aaa2 3 | 1 | aaa3 4 | 2 | ddd1 5 | 2 | ddd2 ... | ... | ... 19 | 3 | fff1 20 | 3 | fff2 21 | 3 | fff3 22 | 3 | fff4 I want to make a SELECT query that will return the first N (e.g. two) rows for each AuthorId, ordering by Title ("Select the first two books of each author"). Sample output: BookId | AuthorId | AuthorName | Title ------------------------------------------ 1 | 1 | Alice | aaa1 2 | 1