greatest-n-per-group

SQL: Filter rows with max value

落花浮王杯 提交于 2019-11-28 08:10:55
问题 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

MySQL select top X records for each individual in table

风格不统一 提交于 2019-11-28 06:39:14
Is there a better way to get multiple "top X" results from a MySQL table? I'm able to accomplish this easily with a union when the number of different foo is small: (SELECT foo,score FROM tablebar WHERE (foo = 'abc') ORDER BY score DESC LIMIT 10) UNION (SELECT foo,score FROM tablebar WHERE (foo = 'def') ORDER BY score DESC LIMIT 10) I could obviously keep adding unions for each value of foo. However, this isn't practical when there are 500+ different values for foo and I need the top X of each. This sort of query can be rephrased in a "greatest-n-per-group" sense, where you want the top 10

SQL - Finding the maximum date group by id table

落爺英雄遲暮 提交于 2019-11-28 05:59:42
问题 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

MYSQL how to select data where a field has a min value

╄→гoц情女王★ 提交于 2019-11-28 05:49:36
Please I want to select data from a table, where a specific field has the min value, I've tried this : SELECT * FROM pieces where min(price) I'm not good with MySQL, please any help ? Thanks this will give you result that has the minimum price on all records. SELECT * FROM pieces WHERE price = ( SELECT MIN(price) FROM pieces ) SQLFiddle Demo This is how I would do it (assuming I understand the question) SELECT * FROM pieces ORDER BY price ASC LIMIT 1 If you are trying to select multiple rows where each of them may have the same price (which is the minimum) then @JohnWoo's answer should suffice

how to sort order of LEFT JOIN in SQL query?

…衆ロ難τιáo~ 提交于 2019-11-28 05:42:48
OK I tried googling for an answer like crazy, but I couldn't resolve this, so I hope someone will be able to help. Let's say I have a table of users, very simple table: id | userName 3 Michael 4 Mike 5 George and I have another table of their cars and their prices. id | belongsToUser | carPrice 1 4 5000 2 4 6000 3 4 8000 Now what I need to do is something like this (feel free to rewrite): SELECT `userName`, `carPrice` FROM `users` LEFT JOIN `cars` ON cars.belongsToUser=users.id WHERE `id`='4' Which returns: Mike | 5000 But I need the most expensive car of a certain user, not the first entry

GROUP BY having MAX date

走远了吗. 提交于 2019-11-28 04:28:41
I have problem when executing this code: SELECT * FROM tblpm n WHERE date_updated=(SELECT MAX(date_updated) FROM tblpm GROUP BY control_number HAVING control_number=n.control_number) Basically, I want to return the most recent date for each control number. The query above returns correct output but it takes 37secs. before the output was shown. Is there any other sql clause or command that can execute faster than the query above? Thanks in advance. Putting the subquery in the WHERE clause and restricting it to n.control_number means it runs the subquery many times. This is called a correlated

Find max per group and return another column

﹥>﹥吖頭↗ 提交于 2019-11-28 03:40:22
问题 Given the following test matrix: testMatrix <- matrix( c(1,1,2,10,20,30,300,100,200,"A","B","C"), 3, 4) colnames(testMatrix) <- c("GroupID", "ElementID", "Value", "Name") Here I want to find the max per group and then return the name of that column. E.g. I would expect 1, A and 2, C. If there is a tie with max, the first match would be fine. After that I would have to attach this to the matrix with a new Column "GroupName" How can I do this? I already have the Group, Max Value combination:

Get values from first and last row per group

喜夏-厌秋 提交于 2019-11-28 03:29:07
问题 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

MySQL Group By to display latest result

99封情书 提交于 2019-11-28 02:19:59
问题 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`,

How to select more than 1 record per day?

瘦欲@ 提交于 2019-11-28 01:28:44
This is a postgresql problem. PostgreSQL 8.3.3 on i686-redhat-linux-gnu, compiled by GCC gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-9). The table looks like: date_time other_column 2012-11-01 00:00:00 ... 2012-11-02 01:00:00 ... 2012-11-02 02:00:00 ... 2012-11-02 03:00:00 ... 2012-11-02 04:00:00 ... 2012-11-03 05:00:00 ... 2012-11-03 06:00:00 ... 2012-11-05 00:00:00 ... 2012-11-07 00:00:00 ... 2012-11-07 00:00:00 ... ... I want to select at most 3 records per day from a specific date range. For example, I want to select at most 3 records from 2012-11-02 to 2012-11-05. The expected result would be