greatest-n-per-group

SQL Select Distinct column and latest date

丶灬走出姿态 提交于 2019-12-22 13:56:28
问题 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

Select most recent rows that match a condition in SQLite

人走茶凉 提交于 2019-12-22 08:28:41
问题 Let's say I have a table: Name, status, timestamp And I want to select the rows that match status='active' but only those that have the most recent timestamp for each of them. So if there were rows like this: Bob, active, 5/10/2010 Bob, active, 6/12/2010 Ann, inactive, 6/12/2000 Ann, active, 9/3/2009 Ann, active, 9/25/2010 I'd want it to return: Bob, active, 6/12/2010 Ann, active, 9/25/2010 How can I do this? I'm using SQLite, if it matters. Thank you. 回答1: select name, status, max(timestamp)

Join single row from a table in MySQL

本秂侑毒 提交于 2019-12-22 08:27:08
问题 I have two tables players and scores . I want to generate a report that looks something like this: player first score points foo 2010-05-20 19 bar 2010-04-15 29 baz 2010-02-04 13 Right now, my query looks something like this: select p.name player, min(s.date) first_score, s.points points from players p join scores s on s.player_id = p.id group by p.name, s.points I need the s.points that is associated with the row that min(s.date) returns. Is that happening with this query? That is, how can I

MYSQL query to find the all employees with nth highest salary

天大地大妈咪最大 提交于 2019-12-21 19:24:34
问题 The two tables are salary_employee and employee employee_salary salary_id emp_id salary Employee emp_id | first_name | last_name | gender | email | mobile | dept_id | is_active Query to get the all employees who have nth highest salary where n =1,2,3,... any integer SELECT a.salary, b.first_name FROM employee_salary a JOIN employee b ON a.emp_id = b.emp_id WHERE a.salary = ( SELECT salary FROM employee_salary GROUP BY salary DESC LIMIT 1 OFFSET N-1 ) My Questions: 1) Is there any better and

Get next minimum, greater than or equal to a given value for each group

落爺英雄遲暮 提交于 2019-12-21 05:07:14
问题 given the following Table1: RefID intVal SomeVal ---------------------- 1 10 val01 1 20 val02 1 30 val03 1 40 val04 1 50 val05 2 10 val06 2 20 val07 2 30 val08 2 40 val09 2 50 val10 3 12 val11 3 14 val12 4 10 val13 5 100 val14 5 150 val15 5 1000 val16 and Table2 containing some RefIDs and intVals like RefID intVal ------------- 1 11 1 28 2 9 2 50 2 51 4 11 5 1 5 150 5 151 need an SQL Statement to get the next greater intValue for each RefID and NULL if not found in Table1 following is the

Select records based on last date

♀尐吖头ヾ 提交于 2019-12-20 17:35:33
问题 Based on the table called Course below: How can I select records which have course name with latest date? I mean if I have two same course names for one ID, I should only show the latest one as the below result. Simply, I want only to show the latest row per ("ID", "Course Name"). And what if I have two date columns in Course table, which are StartDate & EndDate and I want to show the same based on EndDate only.? I am using PostgreSQL. 回答1: In PostgreSQL, to get unique rows for a defined set

Join two tables where table A has a date value and needs to find the next date in B below the date in A

爷,独闯天下 提交于 2019-12-20 07:07:29
问题 I got this table "A": | id | date | =================== | 1 | 2010-01-13 | | 2 | 2011-04-19 | | 3 | 2011-05-07 | | .. | ... | and this table "B": | date | value | ====================== | 2009-03-29 | 0.5 | | 2010-01-30 | 0.55 | | 2011-08-12 | 0.67 | Now I am looking for a way to JOIN those two tables having the "value" column in "B" mapped to the dates in "A". The tricky part for me here is that table "B" only stores the change date and the new value. Now when I need this value in table "A"

Find second highest record from oracle db [duplicate]

戏子无情 提交于 2019-12-20 06:48:07
问题 This question already has answers here : How to get second largest or third largest entry from a table (12 answers) Closed last year . I have the following data: id date mia 1 1/1/2017 3 1 1/2/2017 1 1 1/3/2017 2 2 1/4/2017 1 2 1/5/2017 4 2 1/6/2017 6 . . . . and so on. If I give input as id=1 I should fetch record of 2017-02-01 and if input id=2 then record of 2017-05-01 i.e record of previous month of the highest date. 回答1: You could use: SELECT * FROM (SELECT *, ROW_NUMBER() OVER(PARTITION

How to find previous record [n-per-group max(timestamp) < timestamp]?

耗尽温柔 提交于 2019-12-20 06:26:37
问题 I have a large table containing time series sensor data. Large is anything from a few thousand up to 10M record divided amongst various channels being monitored. For a certain sensor type I need to calculate the time interval between the current and previous reading, i.e. find the largest timestamp prior to the current one. The obvious approaches come to mind, each measured on Core i5 for a channel of 40k entries: Correlated subquery SELECT collect.*, prev.timestamp AS prev_timestamp FROM

How to get minimum date by each records from multiple records

懵懂的女人 提交于 2019-12-20 06:12:29
问题 I would like to get the minimum date of each record in my table having multiple entry of date with one primary key. Take a look at my table: CaseNo Entry_date ABC-001 2/12/13 ABC-002 2/09/13 ABC-001 1/01/13 ABC-001 1/31/13 ABC-002 1/01/13 ABC-003 2/01/12 ABC-003 2/18/13 I want to have this result: CaseNo Entry_date Min_date ABC-001 2/12/13 1/01/13 ABC-002 2/09/13 1/09/13 ABC-001 1/01/13 1/01/13 ABC-001 1/31/13 1/01/13 ABC-002 1/09/13 1/09/13 ABC-003 2/01/12 2/01/13 ABC-003 2/18/13 2/01/13 I