greatest-n-per-group

MySQL find top results for each group

时间秒杀一切 提交于 2019-12-20 05:58:46
问题 I have searched a lot, but I can't find an answer or something near. I have a table like this: id int(11) NO PRI auto_increment attribute_id int(11) YES user_id int(11) YES result int(11) YES x10 int(11) YES I need about 5 results from each attribute_id . attribute_id can be any number, so at first, I need to check for the attribute_id , and next I have to check for the results for each attribute_id. I can't find out how I do this without any programming language other than MySQL, but I know

Group and Select First From Two Tables Linq

时光毁灭记忆、已成空白 提交于 2019-12-20 04:38:43
问题 I'm trying to create a simple query using EFCore, returning the list of people i'm conversing with, and the last message that was sent between the two of us (pretty much like how it's displayed on Facebook Messenger or Whatsapp). I created the linq query but its generating one hell of an sql query. I'm trying to optimize the linq query to generate a better sql, so here comes the full story: 0. The Two Entities: Visitors and ChatMessages The Visitor contains the visitor information, and the

How do I get the record ID of the record with the min date for each foreign key?

丶灬走出姿态 提交于 2019-12-20 03:45:07
问题 I have the following table recordID createdDate ForeignKeyID 00QA000000PtFXaMAN 2012-01-03 13:23:36.000 001A000000ngM21IAE 00QA000000OS2QiMAL 2011-12-15 12:03:02.000 001A000000ngM21IAE . . . . I am trying to get the recordID for foreignKeyID where createdDAte is the min(createdDate) for foreignKeyID if recordID is identity int I can get that by doing the following query Select min(recordId),ForeignkeyID from table group by ForeignKeyId I originaly thought that I can create temp table with the

MySQL SELECT n records base on GROUP BY

耗尽温柔 提交于 2019-12-19 22:04:59
问题 Lets say I have SQL records: Country | Number USA | 300 USA | 450 USA | 500 USA | 100 UK | 300 UK | 400 UK | 1000 And I am doing something like this: SELECT * FROM table GROUP BY Country . What if, I want to choose to display the result with 2 greatest number only in each country? How can I archive this? The result would be: Country | Number USA | 450 USA | 500 UK | 400 UK | 1000 回答1: Sample data create table data (Country varchar(10), Number int); insert into data select 'USA' , 300 union

MySQL SELECT n records base on GROUP BY

雨燕双飞 提交于 2019-12-19 22:04:36
问题 Lets say I have SQL records: Country | Number USA | 300 USA | 450 USA | 500 USA | 100 UK | 300 UK | 400 UK | 1000 And I am doing something like this: SELECT * FROM table GROUP BY Country . What if, I want to choose to display the result with 2 greatest number only in each country? How can I archive this? The result would be: Country | Number USA | 450 USA | 500 UK | 400 UK | 1000 回答1: Sample data create table data (Country varchar(10), Number int); insert into data select 'USA' , 300 union

Inner Join table with respect to a maximum value

送分小仙女□ 提交于 2019-12-19 18:29:30
问题 I'm trying to write a MySQL query where I pull a seller's info and her most popular product. This is determined by the product with the most page views, i.e. MAX(page_views) . The query below though is just pulling a random product and not the one with the most page views. "SELECT seller.id, seller.language, seller.shop_name,seller.story, seller.eng_story, product.id, product.image_thumb, product.title, product.eng_title, product.price, MAX(product.page_views) FROM seller INNER JOIN product

MySQL Greatest N Results with Join Tables

牧云@^-^@ 提交于 2019-12-19 11:38:09
问题 Selecting the Top n Results, I've seen the numerous posts and great articles on here about how to do it but I am struggling to do it with my data set. Most of the examples focus on data sets without the need for additional joins. I've been trying to apply the examples from http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ to my query without much success. Three tables exist Person, Credit and Media. Person links to Credit and Credit to Media. The query

How can I query rankings for the users in my DB, but only consider the latest entry for each user?

自闭症网瘾萝莉.ら 提交于 2019-12-19 09:23:45
问题 Lets say I have a database table called "Scrape" possibly setup like: UserID (int) UserName (varchar) Wins (int) Losses (int) ScrapeDate (datetime) I'm trying to be able to rank my users based on their Wins/Loss ratio. However, each week I'll be scraping for new data on the users and making another entry in the Scrape table. How can I query a list of users sorted by wins/losses, but only taking into consideration the most recent entry (ScrapeDate)? Also, do you think it matters that people

Select distinct rows whilst grouping by max value

不想你离开。 提交于 2019-12-19 06:42:05
问题 I currently have the following table: ID | Name | EventTime | State 1001 | User 1 | 2013/07/22 00:00:05 | 15 1002 | User 2 | 2013/07/23 00:10:00 | 100 1003 | User 3 | 2013/07/23 06:15:31 | 35 1001 | User 1 | 2013/07/23 07:13:00 | 21 1001 | User 1 | 2013/07/23 08:15:00 | 25 1003 | User 3 | 2013/07/23 10:00:00 | 22 1002 | User 2 | 2013/07/23 09:18:21 | 50 What I need is the state for each distinct userid from the last eventtime similar to below: ID | Name | EventTime | State 1001 | User 1 |

SQL Select only rows with Minimum Value on a Column with Where Condition

断了今生、忘了曾经 提交于 2019-12-19 06:41:35
问题 Table: | id | productId | orderIndex | rejected | ------------------------------------------ | 1 | 1 | 0 | 1 | | 2 | 1 | 1 | 0 | | 3 | 1 | 2 | 0 | | 4 | 2 | 0 | 0 | | 5 | 2 | 1 | 1 | | 6 | 3 | 0 | 0 | How can I select one row per productId with minimum orderIndex that not rejected? Expected result: | id | productId | orderIndex | rejected | ------------------------------------------ | 2 | 1 | 1 | 0 | | 4 | 2 | 0 | 0 | | 6 | 3 | 0 | 0 | I tried this query, but don't recieved correct result: