greatest-n-per-group

Get first row for one group [closed]

旧城冷巷雨未停 提交于 2019-12-13 08:46:21
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I want to get the first row for every GV(TeacherID) GV | Class| SUM GV1| L001 | 5000 GV1| L002 | 5000 GV1| L003 | 5000 GV2| L002 | 7000 GV2| L003 | 7000 GV2| L001 | 7000 GV3| L001 | 8000 GV3| L002 | 8000 GV3|

Returning a Min() and another field? [duplicate]

霸气de小男生 提交于 2019-12-13 08:39:46
问题 This question already has answers here : Retrieving the last record in each group - MySQL (26 answers) Closed last year . I'm building a leaderboard and want to return a MIN() value - however, I also want another field which is on the same row as the min value. I have this so far: SELECT u.username, MIN(timer) AS intScore, l.hashtag FROM users u JOIN leaderboard l ON l.users_id = u.users_id GROUP BY u.username ORDER BY intScore ASC ...this returns the correct MIN intScore, but not the correct

Unable to convert SQL which is meant for greatest N per group to HQL

主宰稳场 提交于 2019-12-13 05:49:26
问题 I have two tables in Oracle namely product and product_image . As the names imply they have a one-to-many relationship from product to product_image. The product entity: @Entity @Table(name = "PRODUCT", catalog = "", schema = "WAGAFASHIONDB") public class Product implements java.io.Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @Column(name = "PROD_ID", nullable = false, precision = 35, scale = 0) @SequenceGenerator(name = "productIdSequence",

SQL: How to select the record with the most recent past date for every record on other table

依然范特西╮ 提交于 2019-12-13 05:05:36
问题 How can I reformulate this two queries in one single query on MySQL? SELECT * FROM tvnetwork //this query gives me a list of TV networks But networks usually change names and logos, so for each of the retrieved TV networks: SELECT name, logo FROM tvnetworkinfo WHERE id = $tvnetwork.id AND date_since < NOW() ORDER BY date_since desc LIMIT 1 //this one gives me the most recent logo and name for the network I intentionally leave out the NEXT name/logo change. E.g. I want "NatGeo" instead of the

Optimizing query to get entire row where one field is the maximum for a group

一笑奈何 提交于 2019-12-13 03:23:58
问题 I have a table with a schema like, say, EventTime DATETIME(6), EventType VARCHAR(20), Number1 INT, Number2 INT, Number3 INT, ... There are an unimaginably large number of rows in this table, but for the sake of this query I'm only interested in, say, a few thousand of them that are between two given values of EventTime . There's an index on EventTime , and if I just do something like SELECT * FROM table WHERE EventTime >= time1 and EventTime <= time2; Then it's able to return the relevant

How do I group and order with sql at the same time?

主宰稳场 提交于 2019-12-13 02:05:18
问题 I have got the following sqlite3 table: Name | LastUpdated | Status ============================ Adam | 2011-05-28 | 1 Bob | 2011-05-05 | 6 Adam | 2011-05-27 | 2 Adam | 2011-05-16 | 1 Adam | 2011-05-26 | 3 Bob | 2011-05-18 | 1 Adam | 2011-05-29 | 6 and I want to select the a row per Name ordered by the LastUpdated column. So I want to get this data: Adam | 2011-05-29 | 6 Bob | 2011-05-18 | 1 I think I have to do a subquery, but I can't figure out how to go about it. 回答1: SQLite (and MySQL)

Sort child rows of each group in the desired order and take the desired number of top rows in each group in JPA

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 21:28:55
问题 Is it possible to sort child rows of each group (child rows of each parent row) in the desired order and take the desired number of top rows in each group in JPA? For example, I have three tables in MySQL database. category sub_category product The relationship between these tables is intuitive - one-to-many in the order in which they appear. I'm executing the following criteria query on the sub_category table. CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder(); CriteriaQuery

What SQL query returns the row with the most recent Date and Time (Column B and C) for each unique Column A?

核能气质少年 提交于 2019-12-12 21:22:17
问题 What SQL query returns the row with the most recent Date and Time (Column B and C) for each unique Column A? 回答1: If C is actually a datetime column with date and time information set, you can do: select a, max(c) from table group by a; If B is a date column and C is a time column, then you need: select a, max(convert(varchar(15), b) + ' ' + convert(varchar(15), c)) from table group by a; 回答2: When I do: select convert(varchar, getdate()) I get "Nov 19 2010 5:17PM", which doesn't help when

TOP N problem with GROUP BY clause

谁都会走 提交于 2019-12-12 19:24:10
问题 The problem: I need to find all active [GiftPledges] that have the last three [GiftDetails] have a zero amount. SELECT gp.PledgeId FROM GiftPledge gp INNER JOIN GiftDetail gd ON gp.PledgeId = gd.PledgeId WHERE gp.PledgeStatus = 'A' GROUP BY PledgeId HAVING COUNT(PledgeId) >= 3 Now, I have all my [GiftPledges] that have at least three [GiftDetails]. SELECT TOP 3 gdi.Amt FROM GiftDetail gdi INNER JOIN GiftHeader ghi ON gdi.GiftRef = ghi.GiftRef WHERE gdi.PledgeId = gp.PledgeId ORDER BY ghi

Time series querying in Postgres

爱⌒轻易说出口 提交于 2019-12-12 14:15:47
问题 This is a follow on question from @Erwin's answer to Efficient time series querying in Postgres. In order to keep things simple I'll use the same table structure as that question id | widget_id | for_date | score | The original question was to get score for each of the widgets for every date in a range. If there was no entry for a widget on a date then show the score from the previous entry for that widget. The solution using a cross join and a window function worked well if all the data was