greatest-n-per-group

SQL - Finding the maximum date group by id table

半世苍凉 提交于 2019-11-29 12:11:00
Having a table below, I need to get rows with the maximum date having statut equal 2 REMUN_ID HISTO_ID DATE_MAJ STATUT 2122 7005 08/27/2014 11:10:23 2 1603 5486 08/27/2014 11:10:21 1 2122 5151 08/27/2014 11:08:36 1 1603 4710 08/27/2014 11:08:32 2 I need to get the row with the maximum date and group by REMUN_ID the result using this request select remun_id, max(date_maj) from histo_statut_remun group by remun_id; Result : REMUN_ID DATE_MAJ 2122 08/27/2014 11:10:23 1603 08/27/2014 11:10:21 I need to adjust the request to get only rows with statut = 2 from this result My purpose is to get the

Group-wise Maximum of a Certain Column

青春壹個敷衍的年華 提交于 2019-11-29 11:19:23
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(article); For this query, I get the following result-set; +---------+--------+------------+ | article |

MYSQL shows incorrect rows when using GROUP BY

瘦欲@ 提交于 2019-11-29 11:15:14
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 WHERE ticket.id = article.ticket_id AND ticket.queue_id = 1 GROUP BY article.ticket_id For example,

Get values from first and last row per group

僤鯓⒐⒋嵵緔 提交于 2019-11-29 09:56:37
I'm new to Postgres, coming from MySQL and hoping that one of y'all would be able to help me out. I have a table with three columns: name , week , and value . This table has a record of the names, the week at which they recorded the height, and the value of their height. Something like this: Name | Week | Value ------+--------+------- John | 1 | 9 Cassie| 2 | 5 Luke | 6 | 3 John | 8 | 14 Cassie| 5 | 7 Luke | 9 | 5 John | 2 | 10 Cassie| 4 | 4 Luke | 7 | 4 What I want is a list per user of the value at the minimum week and the max week. Something like this: Name |minWeek | Value |maxWeek | value

MySQL Group By to display latest result

余生长醉 提交于 2019-11-29 08:57:51
I'm trying to query MySQL to ORDER then GROUP... it's a question that comes up a lot here and I found an answer that seemed relevant to me: Getting a MySQL group by query to display the row in that group with the highest value However I'm finding that it is still not ordering before doing the grouping. Specifically what I'm trying to do is use Wordpress custom post types to group by a meta data field called 'date', ordered by the post date. Here's my query: SELECT `ID`, `date`, `post_date`, `date_rank` FROM ( SELECT `Post`.`ID`, `Post`.`post_date`, `PostData`.`meta_value` AS `date`, CASE WHEN

PostgreSQL: top n entries per item in same table

喜夏-厌秋 提交于 2019-11-29 07:43:34
| uId | title | amount | makers | widgets | 1 richard 998 xcorp sprocket 2 swiss 995 ycorp framitz 3 ricky 90 zcorp flobber 4 ricky2 798 xcorp framitz 1 lilrick 390 xcorp sprocket 1 brie 200 mcorp gullywok 1 richard 190 rcorp flumitz 1 brie 490 bcorp sprocket etc... I am trying to retrieve only 3 records per makers , the top 3 amounts and the widgets they produced Here's is what I have: SELECT amount, makers FROM (SELECT amount, makers, (SELECT count(*) FROM entry as t2 WHERE t2.amount = t1.amount and t2.makers >= t1.makers) AS RowNum FROM entry as t1 ) t3 WHERE t3.RowNum<4 order by amount; Is

MySQL retrieve latest record for Group

北慕城南 提交于 2019-11-29 07:17:28
I have a social networking site and am struggling with a query. I have a posts table that holds all of the users posts and then a post_comments table that holds all comments on a post. I am trying to find the latest comment by post from post_comments table. The post_comments table has the following columns: post_comment_id, post_id, writer_user_id, post_comment_content, datetime I have grouped the results by post_id like so: SELECT * FROM post_comments GROUP BY post_id This almost does what I want but it always returns the oldest comment for each post not the newest one. How can I get it to

Select second most minimum value in Oracle

给你一囗甜甜゛ 提交于 2019-11-29 06:57:04
I need to write a query that selects a minimum value and it's second most minimum value from a list of integers. Grabbing the smallest value is obvious: select min(value) from table; But the second smallest is not so obvious. For the record, this list of integers is not sequential -- the min can be 1000, and the second most min can be 10000. Use an analytic function SELECT value FROM (SELECT value, dense_rank() over (order by value asc) rnk FROM table) WHERE rnk = 2 The analytic functions RANK , DENSE_RANK , and ROW_NUMBER are identical except for how they handle ties. RANK uses a sports-style

How can a do a “greatest-n-per-group” query in django?

别说谁变了你拦得住时间么 提交于 2019-11-29 06:52:52
(This is the django version of the thread at SQL join: selecting the last records in a one-to-many relationship ) Suppose I have a table of customers and a table of purchases. Each purchase belongs to one customer. I want to get a list of all customers along with their last purchase. Can it be done without raw SQL and without multiple database queries? Tomasz Zieliński You can take a look at similar discussion: Django Query That Get Most Recent Objects From Different Categories You can't do this in one query in Django. You can get the customer with just the date of their most recent purchase

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