greatest-n-per-group

MYSQL Query - Get latest comment related to the post

穿精又带淫゛_ 提交于 2019-12-19 05:51:29
问题 I am trying to get the latest 1 or 2 comments related to each post I download, a bit like instagram does as they show the latest 3 comments for each post, So far I am getting the posts & the likes counts. Now all I need to do is figure out how to get the latest comments, not too sure how to approach it and that is why I hoping someone with a lot more expertise can help me! This is my current query: (SELECT P.uuid, P.caption, P.imageHeight, P.path, P.date, U.id, U.fullname, U.coverImage, U.bio

SQL Query, Selecting 5 most recent in each group

若如初见. 提交于 2019-12-19 02:32:32
问题 I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `language_id` int(11) unsigned NOT NULL, `title` varchar(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `time_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 language_id refers to what language the record is in. What I would like to do is retrieve a list of the five most recent (ORDER BY time_posted DESC LIMIT 5) records in

one-to-many query selecting all parents and single top child for each parent

旧街凉风 提交于 2019-12-18 10:42:57
问题 There are two SQL tables: Parents: +--+---------+ |id| text | +--+---------+ | 1| Blah | | 2| Blah2 | | 3| Blah3 | +--+---------+ Childs +--+------+-------+ |id|parent|feature| +--+------+-------+ | 1| 1 | 123 | | 2| 1 | 35 | | 3| 2 | 15 | +--+------+-------+ I want to select with single query every row from Parents table and for each one single row from Childs table with relation "parent"-"id" value and the greatest "feature" column value. In this example result should be: +----+------+----+

PostgreSQL - Select row with composite maximum value from 2 columns

。_饼干妹妹 提交于 2019-12-18 09:32:33
问题 I would like to select the best offers for a merchant in PostgreSQL 9.6 according some simple rules: An offer is better than another if its discount value is greater regardless the benefit type If the discount values are equal, then the one with benefit type ALL beats the one with FOOD If both discount and benefit type are the same, then any offer can be selected e.g. pick the first one So best is not just a max() call but a "conditional" max() where another column should be inspected too to

MySQL huge tables JOIN makes database collapse

ぐ巨炮叔叔 提交于 2019-12-18 08:56:33
问题 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

Group-wise Maximum of a Certain Column

流过昼夜 提交于 2019-12-18 07:00:14
问题 I've got the table: SELECT * FROM shop; +---------+--------+------ | article | dealer | price +---------+--------+------ | 0001 | A | 3.45 | 0001 | B | 3.99 | 0002 | A | 10.99 | 0003 | B | 1.45 | 0003 | C | 1.69 | 0003 | D | 1.25 | 0004 | D | 19.95 +---------+--------+------ 7 rows in set (0.20 sec) And I want to get - for each article - the dealer or dealers with the most expensive price. Could anyone tell me why this doesn’t work? SELECT article, dealer, MAX(price) FROM shop GROUP BY

MYSQL shows incorrect rows when using GROUP BY

独自空忆成欢 提交于 2019-12-18 06:56:21
问题 I have two tables: article('id', 'ticket_id', 'incoming_time', 'to', 'from', 'message') ticket('id', 'queue_id') where tickets represent a thread of emails between support staff and customers, and articles are the individual messages that compose a thread. I'm looking to find the article with the highest incoming time (expressed as a unix timestamp) for each ticket_id, and this is the query I'm currently using: SELECT article.* , MAX(article.incoming_time) as maxtime FROM ticket, article

Select multiple (non-aggregate function) columns with GROUP BY

人盡茶涼 提交于 2019-12-17 23:49:31
问题 I am trying to select the max value from one column, while grouping by another non-unique id column which has multiple duplicate values. The original database looks something like: mukey | comppct_r | name | type 65789 | 20 | a | 7n 65789 | 15 | b | 8m 65789 | 1 | c | 1o 65790 | 10 | a | 7n 65790 | 26 | b | 8m 65790 | 5 | c | 1o ... This works just fine using: SELECT c.mukey, Max(c.comppct_r) AS ComponentPercent FROM c GROUP BY c.mukey; Which returns a table like: mukey | ComponentPercent

Retrieve 2 last posts for each category

五迷三道 提交于 2019-12-17 19:55:34
问题 Lets say I have 2 tables: blog_posts and categories. Each blog post belongs to only ONE category, so there is basically a foreign key between the 2 tables here. I would like to retrieve the 2 lasts posts from each category, is it possible to achieve this in a single request? GROUP BY would group everything and leave me with only one row in each category. But I want 2 of them. It would be easy to perform 1 + N query (N = number of category). First retrieve the categories. And then retrieve 2

Conditional lead/lag function PostgreSQL?

拜拜、爱过 提交于 2019-12-17 19:47:00
问题 I have a table like this: Name activity time user1 A1 12:00 user1 E3 12:01 user1 A2 12:02 user2 A1 10:05 user2 A2 10:06 user2 A3 10:07 user2 M6 10:07 user2 B1 10:08 user3 A1 14:15 user3 B2 14:20 user3 D1 14:25 user3 D2 14:30 Now, I need a result like this: Name activity next_activity user1 A2 NULL user2 A3 B1 user3 A1 B2 I would like to check for every user the last activity from group A and what type of activity took place next from group B (activity from group B always takes place after