greatest-n-per-group

I'm trying to load only the last 3 comments on every post

此生再无相见时 提交于 2019-12-24 07:48:48
问题 i want get all posts with last three comment on each post. my relation is public function comments() { return $this->hasMany('App\Commentpostfeed','post_id')->take(3); } This would return only 3 comments total whenever I called it instead of 3 comments per post. i use this way : 1 : Postfeed::with(['comment' => function($query) { $query->orderBy('created_at', 'desc')->take(3); }]); 2 : $postings = Postfeed::with('comments')->get(); but getting same result. please help me out for this problem.

Producing n rows per group

对着背影说爱祢 提交于 2019-12-24 03:25:20
问题 It is known that GROUP BY produces one row per group. I want to produce multiple rows per group. The particular use case is, for example, selecting two cheapest offerings for each item. It is trivial for two or three elements in the group: select type, variety, price from fruits where price = (select min(price) from fruits as f where f.type = fruits.type) or price = (select min(price) from fruits as f where f.type = fruits.type and price > (select min(price) from fruits as f2 where f2.type =

How to get other columns with are not in GROUP BY clause in a oracle select sql?

依然范特西╮ 提交于 2019-12-24 02:49:07
问题 I have a table MOVIE containing this data. MOVIE_ID MOVIE_TITLE CATEGORY SALES_AMT --------- ------------------------ --------------- ---------- M_0000004 The Boss Baby Animation 2000 M_0000006 Star Wars: The Last Jedi Science Fiction 3000 M_0000007 Get Out Horror 4000 M_0000008 Million Dollar Arm Action 2000 M_0000009 The Conjuring Horror 1000 M_0000012 The Dark Knight Action 3000 I need the Top movies data based on SALES_AMT with respect to CATEGORY The required result is this: MOVIE_ID

Retrieve the most recent row - pre- Oracle 12c

久未见 提交于 2019-12-23 23:42:08
问题 Please bear with me as I am new to all this. My query is, I have the following table and I am attempting to retrieve the most recent NOTE_PAD.NOTE_TEXT against the order. Example of how the table looks like is below. The data in the below table is obtained from joining two separate tables. I have used the following syntax for the join and retrieved only a specific order number for this example (867318) as there are many orders in the data base. Not certain if I’ve made it clear enough, but if

Find details for minimum price entry for each group of rows with the same article number

a 夏天 提交于 2019-12-23 23:16:08
问题 I read a lot about this, but none worked for me. Can someone help? I have a big table with a lot of different articles (a lot with same EAN) and need always only the cheapest one (sort by price) with the correct AN: *art price an ean *Test |79,00|15770|0808736558136 *Test |85,00|k3238|0808736558136 *Test |68,00|r4850|0808736558136 *Test |65,00|a1117|0808736558136 *Test |78,00|t8619|0808736558136 Expect this one: *Test |65,00|a1117|0808736558136 回答1: SELECT B.* FROM BigTable AS B -- Why do SQL

most simple “return highscores” SQL Query using userID

笑着哭i 提交于 2019-12-23 20:26:37
问题 High there working at a minimal game on Android I get lost when it comes to sql queries. I'm trying to get a top 100 ranking list and it almost works but not quite I am using this query $sql = "SELECT * FROM WorldGames_table GROUP BY user_id ORDER BY score DESC LIMIT 100"; the thing is that it returns a list of the top 100 scores, in the proper order and containing only 1 score of each user. But it is not always the best score of the users that is shown. I am getting completely confused by

postgres: get top n occurrences of a value within each group

随声附和 提交于 2019-12-23 19:46:48
问题 I have a simple table like this: user letter -------------- 1 A 1 A 1 B 1 B 1 B 1 C 2 A 2 B 2 B 2 C 2 C 2 C I want to get the top 2 occurrences of 'letter' per user, like so user letter rank(within user group) -------------------- 1 B 1 1 A 2 2 C 1 2 B 2 or even better: collapsed into columns user 1st-most-occurrence 2nd-most-occurrence 1 B A 2 C B How can I accomplish this in postgres? 回答1: with cte as ( select t.user_id, t.letter, row_number() over(partition by t.user_id order by count(*)

“greatest-n-per-group” query in Django 2.0?

三世轮回 提交于 2019-12-23 15:39:06
问题 Basically, I want to do this but with django 2.0. If I try: Purchases.objects.filter(.....).annotate(my_max=Window( expression=Max('field_of_interest'), partition_by=F('customer') ) ) I get back all the rows but with the my_max property added to each record. 回答1: If you are using PostgreSQL: Purchases.objects.filter(.....).order_by( 'customer', '-field_of_interest' ).distinct('customer') or with Window expression Purchases.objects.filter(.....).annotate(my_max=Window( expression=Max('field_of

TOP 1 Query from each ID with multiple instances

折月煮酒 提交于 2019-12-23 12:51:57
问题 This query will return the top for all rows in MS Access. SELECT TOP 1 * FROM [table] ORDER BY table.[Date] DESC; I need to return the top date for each id that can have multiple dates. ID DATE 1 01/01/2001 1 01/12/2011 3 01/01/2001 3 01/12/2011 Should return only the top dates like this. 1 01/12/2011 3 01/12/2011 回答1: You'll want to use the MAX function, along with a GROUP BY. SELECT ID, MAX(DATE) FROM [table] GROUP BY ID 来源: https://stackoverflow.com/questions/9038980/top-1-query-from-each

select rows satisfying some criteria and with maximum value in a certain column

北战南征 提交于 2019-12-23 12:33:33
问题 I have a table of metadata for updates to a software package. The table has columns id, name, version . I want to select all rows where the name is one of some given list of names and the version is maximum of all the rows with that name. For example, given these records: +----+------+---------+ | id | name | version | +----+------+---------+ | 1 | foo | 1 | | 2 | foo | 2 | | 3 | bar | 4 | | 4 | bar | 5 | +----+------+---------+ And a task "give me the highest versions of records "foo" and