greatest-n-per-group

Need To Pull Most Recent Record By Timestamp Per Unique ID

≡放荡痞女 提交于 2019-12-06 15:04:32
I'm going to apologize up front, this is my first question on stackoverflow... I am attempting to query a table of records where each row has a VehicleID, latitude, longitude, timestamp and various other fields. What I need is to only pull the most recent latitude and longitude for each VehicleID. edit: removed the term unique ID as apparently I was using it incorrectly. If the Unique ID is truely unique, then you will always have the most recent latitude and longitude, because the ID will change with every singe row. If the Unique ID is a Foreign Key (or an ID referencing a unique ID from a

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

感情迁移 提交于 2019-12-06 13:23:12
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 at a bit of a loss. I could just throw the above into find_by_sql , but that just feels dirty. I tried

SQL - Select 'n' greatest elements in group

懵懂的女人 提交于 2019-12-06 12:22:56
问题 The SQL MAX aggregate function will allow you to select the top element in a group. Is there a way to select the top n elements for each group? For instance, if I had a table of users that held their division rank, and wanted the top two users per division ... Users userId | division | rank 1 | 1 | 1 2 | 1 | 2 3 | 1 | 3 4 | 2 | 3 I would want the query to somehow return users 2,3,4 If it matters, I'm using MySQL. 回答1: select * from users as t1 where (select count(*) from users as t2 where t1

retrieve the last inserted row from each user in database

落爺英雄遲暮 提交于 2019-12-06 09:49:36
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/2012 15:00 I want to make a query to get the last inserted rows from chris,john and ina ordered by

SQL Select Distinct column and latest date

元气小坏坏 提交于 2019-12-06 09:47:20
I'm looking to select just the latest records of a table based on date, but only one one Distinct listing of each of the urls. The table structure is like this; ID URL DateVisited 1 google.com 01-01-2016 2 yahoo.com 01-02-2016 3 google.com 12-30-2015 4 google.com 02-01-2016 So for my result set I would want google.com 02-01-2016 yahoo.com 01-02-2016 I will have a couple more conditionals in my actual query, but just want to get the single latest records in a hit log, rather than list of distinct urls and dates, just distinct url's and the latest date. This is actually pretty easy to do using

How to max(date) and use the in feature in sql server in one query?

风流意气都作罢 提交于 2019-12-06 09:32:58
I have a table such as this id color shade date --- ---- ----- ----- 1 red dark 01/01/1990 2 red light 09/16/2013 3 green light 08/15/2010 4 green dark 09/18/2012 5 maroon dark 08/20/2013 6 white dark 08/31/2013 7 white light 08/30/2012 8 purple light 08/20/2010 I wanted entries for each color with the latest date. So I tried doing: select id, color, shade, max(date) from mytable; This didn't work and gave me error: is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Alright, so I added it in the Group By as suggested by the error

mysql select 2 row from each group by

爱⌒轻易说出口 提交于 2019-12-06 08:03:20
问题 i have 2 table with this structure Products id title ----------------- 1 sample 1 2 sample 2 3 sample 3 4 sample 4 5 sample 5 6 sample 6 gallery id typeid name ------------------------------- 1 1 sample for 1 2 1 sample for 1 3 1 sample for 1 4 2 sample for 2 5 2 sample for 2 7 2 sample for 2 8 3 sample for 3 9 3 sample for 3 10 3 sample for 3 11 4 sample for 4 12 4 sample for 4 13 5 sample for 5 14 5 sample for 5 and iwant this for lists of id eg(1,2,3) id typeid name --------------------- 1

How do I take a DISTINCT ON subquery that is ordered by a separate column, and make it fast?

时光毁灭记忆、已成空白 提交于 2019-12-06 07:49:42
问题 (AKA - With a query and data very similar to question "Selecting rows ordered by some column and distinct on another", how can I get it to run fast). Postgres 11. I have table prediction with (article_id, prediction_date, predicted_as, article_published_date) that represents the output from a classifier over a set of articles. New articles are frequently added to a separate table (Represented by the FK article_id ), and new predictions are added as we tune our classifier. Sample data: | id |

Get back the second latest date per ID instead of the latest

廉价感情. 提交于 2019-12-06 07:28:40
So I have an issue with SQlite . I have a table results . I want to write a query now that gives me back not the latest, but the row after that. Let's take a look on an example: ID,searchID,hit,time 1,1,3,1-1-2008 1,1,8,1-1-2009 1,1,4,1-1-2010 1,2,9,1-1-2011 1,2,10,1-1-2009 and I want to get back one time per searchID now (the pre-latest): 1,1,8,1-1-2009 1,2,10,1-1-2009 It is really easy to do it with the last time SELECT searchID, hit, max(time) FROM results group BY searchID But I need the pre-latest for some reasons. PS: this one I found What is the simplest SQL Query to find the second

Select top N rows out of M groups

浪子不回头ぞ 提交于 2019-12-06 06:48:59
问题 I have this table: CREATE TABLE IF NOT EXISTS `catalog_sites` ( `id` int(10) unsigned NOT NULL auto_increment, `cat_id` int(10) unsigned NOT NULL, `date` datetime NOT NULL, `url` varchar(255) NOT NULL, `title` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `keywords` varchar(255) NOT NULL, `visited` int(10) unsigned NOT NULL, `shown` int(10) unsigned NOT NULL, `meta_try` int(1) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `url` (`url`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO