greatest-n-per-group

Using LIMIT within GROUP BY to get N results per group?

南笙酒味 提交于 2019-12-11 16:57:11
问题 The following query: SELECT year, id, rate FROM h WHERE year BETWEEN 2000 AND 2009 AND id IN (SELECT rid FROM table2) GROUP BY id, year ORDER BY id, rate DESC yields: year id rate 2006 p01 8 2003 p01 7.4 2008 p01 6.8 2001 p01 5.9 2007 p01 5.3 2009 p01 4.4 2002 p01 3.9 2004 p01 3.5 2005 p01 2.1 2000 p01 0.8 2001 p02 12.5 2004 p02 12.4 2002 p02 12.2 2003 p02 10.3 2000 p02 8.7 2006 p02 4.6 2007 p02 3.3 What I'd like is only the top 5 results for each id: 2006 p01 8 2003 p01 7.4 2008 p01 6.8 2001

Optimizing query that looks at a specific time window each day

*爱你&永不变心* 提交于 2019-12-11 15:55:46
问题 This is a followup to my previous question Optimizing query to get entire row where one field is the maximum for a group I'll change the names from what I used there to make them a little more memorable, but these don't represent my actual use-case (so don't estimate the number of records from them). I have a table with a schema like this: OrderTime DATETIME(6), Customer VARCHAR(50), DrinkPrice DECIMAL, Bartender VARCHAR(50), TimeToPrepareDrink TIME(6), ... I'd like to extract the rows from

Greatest n-per-group With Multiple Joins

老子叫甜甜 提交于 2019-12-11 15:41:49
问题 Evening, I am trying to get an output of rows that are limited to n per group in MySQL. I can get it to work without joins, but with it I am just shy. I've pasted a dump of the relevant tables here: http://pastebin.com/6F0v1jhZ The query I am using is: SELECT title, catRef, RowNum, pCat, tog FROM ( SELECT title, catRef, @num := IF(@prevCat=catRef,@num+1,1) AS RowNum, @prevCat AS tog, @prevCat := catRef AS pCat FROM (select @prevCat:=null) AS initvars CROSS JOIN ( SELECT p.title, oi.catRef

MySQL Greatest N Per Group Query Hangs

為{幸葍}努か 提交于 2019-12-11 15:37:34
问题 I have a table with columns PredCustId, StartDT and EndDT. For a given StartDT, there can be multiple PredCustIds. Here's what this looks like For each unique StartDT, I would like to retrieve the row with the largest PredCustId. I am specifically trying to implement the left-join solution as seen here but the query hangs every time I run it and I don't understand why. This works SELECT a.* FROM PredCusts AS a LEFT OUTER JOIN PredCusts AS b ON a.StartDT = b.StartDT; but this hangs SELECT a.*

Top Values in a Query by Group

纵饮孤独 提交于 2019-12-11 14:09:47
问题 I know how to get the top values but am having trouble with something very simple. I have a student table. It has: name numberoflaps grade I want the get a query or report that shows the top two kids with the most laps per grade. 回答1: Using MySQL: MySQL doesn't have any ranking functionality, but it does allow for variable creation & updating: SELECT x.grade, x.name, x.numberoflaps FROM (SELECT s.grade, s.name, s.numberoflaps, CASE WHEN @grade != s.grade THEN @rownum := 1 ELSE @rownum :=

SELECT 3 records per user group by on two columns according to view count

怎甘沉沦 提交于 2019-12-11 12:17:48
问题 I've been stuck in a complex MySQL query. Here is my table: +---------------------------------------------------+ | id | user_id | category_id | post_id | view_count | +---------------------------------------------------+ | 1 | 23 | 5 | 213 | 0 | | 2 | 23 | 5 | 214 | 0 | | 3 | 23 | 5 | 215 | 0 | | 4 | 23 | 5 | 216 | 0 | | 5 | 23 | 6 | 217 | 0 | | 6 | 23 | 6 | 218 | 0 | | 7 | 23 | 6 | 219 | 0 | | 8 | 23 | 6 | 220 | 0 | | 9 | 55 | 13 | 221 | 0 | | 10 | 55 | 13 | 222 | 0 | | 11 | 55 | 16 | 223 |

In one to many relationship, return distinct rows based on MIN value

混江龙づ霸主 提交于 2019-12-11 12:08:00
问题 Let's say a patient makes many visits. I want to write a query that returns distinct patient rows based on their earliest visit. For example, consider the following rows. patients ------------- id name 1 Bob 2 Jim 3 Mary visits ------------- id patient_id visit_date reference_number 1 1 6/29/14 09f3be26 2 1 7/8/14 34c23a9e 3 2 7/10/14 448dd90a What I want to see returned by the query is: id name first_visit_date reference_number 1 Bob 6/29/14 09f3be26 2 Jim 7/10/14 448dd90a What I've tried

Select top n records from each category within same table

非 Y 不嫁゛ 提交于 2019-12-11 12:05:23
问题 I've a purchase detail table that has item id , purchase date , and item unit cost . I want to get an avg of an item purchase cost by selecting latest top 2 records from each item id. Item id, purchase date, unitprice 1 3/1/2012 10 1 3/11/2012 8 2 3/1/2012 10 2 3/11/2012 10 1 2/1/2012 9 3 3/1/2012 10 3 3/11/2012 1 3 3/12/2012 13 I'm using sql server 2008 r2 回答1: Try this: ;WITH CTE AS ( SELECT [Item id], [purchase date], unitprice, ROW_NUMBER() OVER(PARTITION BY [Item id] ORDER BY [purchase

How to group following rows by not unique value

大兔子大兔子 提交于 2019-12-11 11:46:49
问题 I have data like this: table1 _____________ id way time 1 1 00:01 2 1 00:02 3 2 00:03 4 2 00:04 5 2 00:05 6 3 00:06 7 3 00:07 8 1 00:08 9 1 00:09 I would like to know in which time interval I was on which way: desired output _________________ id way from to 1 1 00:01 00:02 3 2 00:03 00:05 6 3 00:06 00:07 8 1 00:08 00:09 I tried to use a window function: SELECT DISTINCT first_value(id) OVER w AS id, first_value(way) OVER w as way, first_value(time) OVER w as from, last_value(time) OVER w as to

Deterministic sort order for window functions

别说谁变了你拦得住时间么 提交于 2019-12-11 11:08:42
问题 I've a status table and I want to fetch the latest details. Slno | ID | Status | date 1 | 1 | Pass | 15-06-2015 11:11:00 - this is inserted first 2 | 1 | Fail | 15-06-2015 11:11:00 - this is inserted second 3 | 2 | Fail | 15-06-2015 12:11:11 - this is inserted first 4 | 2 | Pass | 15-06-2015 12:11:11 - this is inserted second I use a window function with partition by ID order by date desc to fetch the first value. Excepted Output : 2 | 1 | Fail | 15-06-2015 11:11:00 - this is inserted second