greatest-n-per-group

DQL Select every rows having one column's MAX value

天涯浪子 提交于 2019-11-30 15:38:57
Working with Symfony 2 and Doctrine, I'm searching for a way to select every rows having the max value in a specific column. Right now, I'm doing it in two queries: One to get the max value of the column in the table Then I select rows having this value. I'm sure this can be done with one query. Searching, I have found this answer in a thread , that seems to be what I am searching for, but in SQL. So according to the answer's first solution, the query I'm trying to build would be something like that: select yt.id, yt.rev, yt.contents from YourTable yt inner join( select id, max(rev) rev from

Laravel 5 eager loading with limit

谁说我不能喝 提交于 2019-11-30 15:12:47
问题 I have two tables, say "users" and "users_actions", where "users_actions" has an hasMany relation with users: users id | name | surname | email... actions id | id_action | id_user | log | created_at Model Users.php class Users { public function action() { return $this->hasMany('Action', 'user_id')->orderBy('created_at', 'desc'); } } Now, I want to retrieve a list of all users with their LAST action . I saw that doing Users::with('action')->get(); can easily give me the last action by simply

Select the SECOND LAST record in each group

别等时光非礼了梦想. 提交于 2019-11-30 14:29:01
There is a table Remark that contains data as shown below: SerialNo | RemarkNo | Desp ============================================= 10 | 1 | rainy 10 | 2 | sunny 11 | 1 | sunny 11 | 2 | rainy 11 | 3 | cloudy 11 | 4 | sunny 12 | 1 | rainy What query will return the following result: 10 | 1 | rainy 11 | 3 | cloudy 12 | null | null That is, the second last record in each group should be returned? Assuming all the RemarkNo for a SerialNo are continuous. The larger the remark number, the later the remark was made. Hence, the second last RemarkNo for SerialNo 10 is 1 with Desp 'rainy'. Try: select s

Laravel 5 eager loading with limit

谁都会走 提交于 2019-11-30 13:51:59
I have two tables, say "users" and "users_actions", where "users_actions" has an hasMany relation with users: users id | name | surname | email... actions id | id_action | id_user | log | created_at Model Users.php class Users { public function action() { return $this->hasMany('Action', 'user_id')->orderBy('created_at', 'desc'); } } Now, I want to retrieve a list of all users with their LAST action . I saw that doing Users::with('action')->get(); can easily give me the last action by simply fetching only the first result of the relation: foreach ($users as $user) { echo $user->action[0]-

Optimal performing query for latest record for each N

此生再无相见时 提交于 2019-11-30 13:44:26
问题 Here is the scenario I find myself in. I have a reasonably big table that I need to query the latest records from. Here is the create for the essential columns for the query: CREATE TABLE [dbo].[ChannelValue]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [UpdateRecord] [bit] NOT NULL, [VehicleID] [int] NOT NULL, [UnitID] [int] NOT NULL, [RecordInsert] [datetime] NOT NULL, [TimeStamp] [datetime] NOT NULL ) ON [PRIMARY] GO The ID column is a Primary Key and there is a non-Clustered index on VehicleID

How do I select TOP 5 PERCENT from each group?

雨燕双飞 提交于 2019-11-30 12:38:17
I have a sample table like this: CREATE TABLE #TEMP(Category VARCHAR(100), Name VARCHAR(100)) INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'John') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP VALUES('A', 'Adam') INSERT INTO #TEMP VALUES('A', 'Lisa') INSERT INTO #TEMP VALUES('A', 'Lisa') INSERT INTO #TEMP VALUES('A', 'Bucky') INSERT INTO

How can I select the set of rows where each item has the greatest timestamp?

谁说胖子不能爱 提交于 2019-11-30 09:44:31
问题 Using Sqlite, I'd like to fetch the collection of rows each with the greatest timestamp. The table contains the properties of items, which are key-value pairs and timestamp. I'd like to select the most recent value for each property. Consider the following simplified schema and data: CREATE TABLE Properties (thing VARCHAR, key VARCHAR, value VARCHAR, timestamp INT); INSERT INTO Properties VALUES ("apple", "color", "red", 0); INSERT INTO Properties VALUES ("apple", "taste", "sweet", 0); INSERT

SQL Query to select bottom 2 from each category

天涯浪子 提交于 2019-11-30 09:42:35
In Mysql, I want to select the bottom 2 items from each category Category Value 1 1.3 1 4.8 1 3.7 1 1.6 2 9.5 2 9.9 2 9.2 2 10.3 3 4 3 8 3 16 Giving me: Category Value 1 1.3 1 1.6 2 9.5 2 9.2 3 4 3 8 Before I migrated from sqlite3 I had to first select a lowest from each category, then excluding anything that joined to that, I had to again select the lowest from each category. Then anything equal to that new lowest or less in a category won. This would also pick more than 2 in case of a tie, which was annoying... It also had a really long runtime. My ultimate goal is to count the number of

Get first/last n records per group by

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 09:32:19
I have two tables : tableA (idA, titleA) and tableB (idB, idA, textB) with a one to many relationship between them. For each row in tableA, I want to retrieve the last 5 rows corresponding in tableB (ordered by idB). I've tried SELECT * FROM tableA INNER JOIN tableB ON tableA.idA = tableB.idA LIMIT 5 but it's just limiting the global result of INNER JOIN whereas I want to limit the result for each different tableA.id How can I do that ? Thanks Karol I think this is what you need: SELECT tableA.idA, tableA.titleA, temp.idB, temp.textB FROM tableA INNER JOIN ( SELECT tB1.idB, tB2.idA, ( SELECT

Optimal performing query for latest record for each N

依然范特西╮ 提交于 2019-11-30 08:38:06
Here is the scenario I find myself in. I have a reasonably big table that I need to query the latest records from. Here is the create for the essential columns for the query: CREATE TABLE [dbo].[ChannelValue]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [UpdateRecord] [bit] NOT NULL, [VehicleID] [int] NOT NULL, [UnitID] [int] NOT NULL, [RecordInsert] [datetime] NOT NULL, [TimeStamp] [datetime] NOT NULL ) ON [PRIMARY] GO The ID column is a Primary Key and there is a non-Clustered index on VehicleID and TimeStamp CREATE NONCLUSTERED INDEX [IX_ChannelValue_TimeStamp_VehicleID] ON [dbo].[ChannelValue] (