greatest-n-per-group

Oracle group using min date

 ̄綄美尐妖づ 提交于 2019-12-23 04:28:32
问题 I have a statement like this: select REFNUMBER, SomeDate, Somecolumn1, Somecolumn2 from Table How can I select the row associated with the lowest date grouped by REFNUMBER ? 回答1: Use the ROW_NUMBER() analytic function: SELECT * FROM ( SELECT REFNUMBER, SomeDate, Somecolumn1, Somecolumn2, ROW_NUMBER() OVER ( PARTITION BY REFNUMBER ORDER BY SomeDate ) As rn FROM Table ) WHERE rn = 1 回答2: Use the first/last aggregate function and avoid a subquery: select refnumber, min(somedate) as somedate, min

Using DISTINCT inside JOIN is creating trouble [duplicate]

血红的双手。 提交于 2019-12-23 04:01:20
问题 This question already has an answer here : Closed 6 years ago . Possible Duplicate: How can I modify this query with two Inner Joins so that it stops giving duplicate results? I'm having trouble getting my query to work. SELECT itpitems.identifier, itpitems.name, itpitems.subtitle, itpitems.description, itpitems.itemimg, itpitems.mainprice, itpitems.upc, itpitems.isbn, itpitems.weight, itpitems.pages, itpitems.publisher, itpitems.medium_abbr, itpitems.medium_desc, itpitems.series_abbr,

select the most recent entry

╄→гoц情女王★ 提交于 2019-12-23 03:59:12
问题 I have the following table: LOCATION_ID, PERSON_ID, DATE 3, 65, 2016-06-03 7, 23, 2016-10-28 3, 23, 2016-08-05 5, 65, 2016-07-14 I want to build a select query in PL/SQL to select the records with the most recent location_id per person_id. For the above sample, the desired result should be: LOCATION_ID, PERSON_ID, DATE 5, 65, 2016-07-14 7, 23, 2016-10-28 (DATE expressed as 'YYYY-MM-DD') Thank you! 回答1: The other proposals are correct but the most compact and fastest solution is most likely

Get most recent Price for each Item

核能气质少年 提交于 2019-12-23 03:38:15
问题 I have a table: ItemID PurchaseDate Price 001 03/17/2013 19.00 002 03/17/2013 14.00 001 03/18/2013 13.00 002 03/18/2013 15.00 001 03/19/2013 17.00 003 03/19/2013 19.00 I need to write a SQL query to get the Price corresponding to the latest PurchaseDate for each ItemID . Entries in table might not necessarily be entered ordered by date Like this: ItemID PurchaseDate Price 001 03/19/2013 17.00 002 03/18/2013 15.00 003 03/19/2013 19.00 回答1: The idea behind the subquery is it separately gets the

Select only newest grouped entries

耗尽温柔 提交于 2019-12-23 03:15:44
问题 I have a table with data like this: +-----------+-------+------+----------+ | timestamp | event | data | moreData | +-----------+-------+------+----------+ | 100000000 | 1 | 10 | 20 | | 100000001 | 1 | 15 | 10 | | 100000002 | 1 | 30 | 30 | | 100000003 | 1 | 5 | 50 | | 100000004 | 2 | 110 | 120 | | 100000005 | 2 | 115 | 110 | | 100000006 | 2 | 130 | 130 | | 100000007 | 2 | 15 | 150 | +-----------+-------+------+----------+ Now I want to select only the newest rows for each event. So in the end

how to write sql query to select rows with max value in one column

安稳与你 提交于 2019-12-23 03:04:25
问题 My Table looks like this. Id | Name | Ref | Date | From 10 | Ant | 100 | 2017-02-02 | David 10 | Ant | 300 | 2016-01-01 | David 2 | Cat | 90 | 2017-09-09 | David 2 | Cat | 500 | 2016-02-03 | David 3 | Bird | 150 | 2017-06-28 | David This is the result I want. Id | Name | Ref | Date | From 3 | Bird | 150 | 2017-06-28 | David 2 | Cat | 500 | 2016-02-03 | David 10 | Ant | 300 | 2016-01-01 | David My target is the highest Ref per Id, ordered by Order Date desc. Could you please tell me about how

MySQL world database Trying to avoid subquery

与世无争的帅哥 提交于 2019-12-23 02:56:40
问题 I'm using the MySQL WORLD database. For each Continent, I want to return the Name of the country with the largest population. I was able to come up with a query that works. Trying to find another query that uses join only and avoid the subquery. Is there a way to write this query using JOIN? SELECT Continent, Name FROM Country c1 WHERE Population >= ALL (SELECT Population FROM Country c2 WHERE c1.continent = c2.continent); +---------------+----------------------------------------------+ |

SQL distinct multiple columns

廉价感情. 提交于 2019-12-23 02:42:27
问题 I have a table as following: book_id author_id mark year 1 1 BAD 2014 1 1 MEDIUM 2014 1 1 GREAT 2015 I would like to execute a query that will give me the best book for each author. Something like this: book_id author_id mark year 1 1 GREAT 2015 I tried to use the distinct keyword on multiple fields - but when I do this: select distinct book_id, author_id from Books I get only the book_id and the author_id (as expected) - but I also need the mark and the year - but I cannot add it to the

ActiveRecord nested SELECT — can I do it without manual SQL?

夙愿已清 提交于 2019-12-22 16:08:07
问题 I have a table with (among other things) a name and a rank. I'd like to return the set of all unique names, but for each name returned, I'd like to pick the row with the highest rank. This is simple with two nested SELECT statements: SELECT * FROM (SELECT * FROM foo ORDER BY rank DESC) AS ordered GROUP BY name MySQL takes the first "hit" for each name, which (because of the earlier ORDER BY) will always be the highest-ranking one. Now, if I want to wire into this table with ActiveRecord, I'm

retrieve the last inserted row from each user in database

安稳与你 提交于 2019-12-22 14:28:23
问题 I want to make a query in mysql with php and get the last inserted row from each user i have in a database. Let me give you an example. Suppose we have a table with 10 rows ID Message User Date inserted 1 lala1 chris 13/02/2010 12:13 2 lala2 john 14/02/2010 12:14 3 lala3 george 15/03/2009 12:00 4 lala4 jack 01/04/2013 11:09 5 lala5 ina 12/08/2012 15:00 6 lala6 chris 13/03/2010 12:13 7 lala7 john 14/01/2010 12:04 8 lala8 george 15/02/2009 12:00 9 lala9 jack 01/03/2013 11:09 10 lala10 ina 12/05