greatest-n-per-group

HAVING - GROUP BY to get the latest record

我怕爱的太早我们不能终老 提交于 2020-01-05 10:11:48
问题 I have a table called RESULTS like this : RESULTS_IDN NAME SUBJECT YEAR QUALIFIED 1 MARK ENGLISH 1989 N 3 MARK ENGLISH 1991 N 5 MARK ENGLISH 1993 Y 7 MARK ENGLISH 1995 N 2 MARK MATH 1990 N 5 MARK MATH 1993 N 6 MARK MATH 1995 Y 4 MARK SCIENCE 1991 N 9 MARK SCIENCE 1997 Y I need to know the Qualification Status of the CANDIDATE for a SUBJECT for the LATEST exam he has written , how do I write a query for this (ORACLE/MSSQL) ? For example Input NAME,SUBJECT OUTPUT NAME IDN SUBJECT YEAR Q MARK

mySQL “get TOP 100 scores” query is turning me crazy

微笑、不失礼 提交于 2020-01-05 05:39:10
问题 I have asked this question Friday and since then I have been trying everything I could think of with no success I cannot get this to work SELECT * FROM WorldFlowers_table GROUP BY device_id ORDER BY score DESC LIMIT 100 and return me : the 100 rows with the top 100 scores filtered so that only the highest score of each device_id is featured ORDER BY score DESC @ 1000111 and Giorgos Betsos A guy comes into a room, says, "please help, I'm getting crazy trying with all my forces to solve a

How to select the row of a minimum value of a group, while also using a where clause

我只是一个虾纸丫 提交于 2020-01-04 05:37:08
问题 I have a table that tracks attendance in a course. The columns are the courseid, lesson, personid, and date. I have a query (below) that extracts the earliest date a person appears along with the associated course, lesson, and personid. This is used to determine when a person started a particular course and ensure they started with the first lesson. This works fine, but where I am stuck is running this query per course. For example, finding the first date each person in a particular course

Limit number of rows per group from join (NOT to 1 row)

荒凉一梦 提交于 2020-01-04 04:10:05
问题 Given these tables: TABLE Stores ( store_id INT, store_name VARCHAR, etc ); TABLE Employees ( employee_id INT, store_id INT, employee_name VARCHAR, currently_employed BOOLEAN, etc ); I want to list the 15 longest-employed employees for each store (let's say the 15 with lowest employee_id ), or ALL employees for a store if there are 15 who are currently_employed='t' . I want to do it with a join clause. I've found a lot of examples of people doing this only for 1 row, usually a min or max

Laravel 4 Query Builder - groupBy Max Date

我的未来我决定 提交于 2020-01-04 02:00:12
问题 little query question, I have table: id | user_id | paper_update ------------------------------ 1 | 1 | 30-5-2011 2 | 2 | 30-5-2012 3 | 3 | 30-5-2012 4 | 1 | 30-5-2013 5 | 2 | 30-5-2013 6 | 3 | 30-5-2014 7 | 4 | 30-5-2014 8 | 5 | 30-5-2014 9 | 5 | 30-5-2015 10 | 5 | 30-5-2016 11 | 1 | 30-5-2010 ------------------------------- What I'm looking to do is to select only the records where paper_update is max between records with the same user_id , actually I want to group by the user_id in order

getting first row in postgres query

江枫思渺然 提交于 2020-01-03 20:04:16
问题 I am querying some data from 2 tables using inner join. here is the query, test_db=> select api_booking.install_ts, api_user.id from api_booking inner join api_user on api_booking.user_id=api_user.id and api_booking.status='completed' limit 20 ; install_ts | id -------------------------------+---- 2016-09-15 09:53:42.511367+00 | 9 2016-10-12 11:37:11.438715+00 | 9 2016-10-21 08:55:49.57813+00 | 9 2017-02-27 06:12:17.362996+00 | 9 2017-02-27 06:24:59.316051+00 | 9 2017-02-28 06:15:35.00841+00

Mysql alternative for LIMIT inside subquery in mysql 5.1.49

情到浓时终转凉″ 提交于 2020-01-03 18:32:22
问题 SELECT student_id FROM `students` AS s1 WHERE student_id IN (SELECT s2.student_id FROM `students` AS s2 WHERE s1.year_of_birth = s2.year_of_birth LIMIT 10) Can't process this query on my server. It drops errors, that says that this version of mysql doesn't support limit inside subqueries etc(ERROR 1235). Is there any solution for my version of mysql 5.1.49? SELECT id, region FROM ( SELECT region, id, @rn := CASE WHEN @prev_region = region THEN @rn + 1 ELSE 1 END AS rn, @prev_region := region

How to get the latest message in each conversation of a certain user in SQL?

旧街凉风 提交于 2020-01-03 18:03:13
问题 I need to write a query that returns the latest message in a conversation between two users. I've included the schema and my (failed) attempts in this fiddle: http://sqlfiddle.com/#!15/322c3/11 I've been working around the problem for some time now but every time I run any of my ugly queries a sweet little kitten dies . Any help would be much appreciated. Do it for the kittens . 回答1: ... the latest message in a conversation between two users. Assuming the users with ID 1 and 3 , like you did

In mongo, how do I use map reduce to get a group by ordered by most recent

孤者浪人 提交于 2020-01-03 17:17:10
问题 the map reduce examples I see use aggregation functions like count, but what is the best way to get say the top 3 items in each category using map reduce. I'm assuming I can also use the group function but was curious since they state sharded environments cannot use group(). However, I'm actually interested in seeing a group() example as well. 回答1: For the sake of simplification, I'll assume you have documents of the form: {category: <int>, score: <int>} I've created 1000 documents covering

SQL Server - pull X random records per state

时光怂恿深爱的人放手 提交于 2020-01-03 08:14:06
问题 I have a table with records for each zip code in the united states. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing this? 回答1: Use: WITH sample AS ( SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.state ORDER BY NEWID()) AS rank FROM ZIPCODES t) SELECT s.* FROM sample s WHERE s.rank <= 5 回答2: SELECT * FROM ZipCodes ORDER BY NEWID() 来源: https://stackoverflow.com/questions/3998573/sql-server-pull-x-random-records-per-state