greatest-n-per-group

selecting a column based on a minimum value of another column

北城余情 提交于 2019-12-01 08:56:42
I have the following table. test_type | brand | model | band | firmware_version | avg_throughput -----------+---------+--------+------+-----------------+---------------- 1client | Linksys | N600 | 5ghz | 1 | 66.94 1client | Linksys | N600 | 5ghz | 2 | 94.98 1client | Linksys | N600 | 5ghz | 4 | 132.40 1client | Linksys | EA6500 | 5ghz | 1 | 216.46 1client | Linksys | EA6500 | 5ghz | 2 | 176.79 1client | Linksys | EA6500 | 5ghz | 4 | 191.44 I'd like to select the avg_throughput of each model that has the lowest firmware_version . When I do SELECT test_type, model, min(firmware_version) FORM

PostgreSQL: select top three in each group

最后都变了- 提交于 2019-12-01 08:43:43
问题 following is my sample table and rows create table com (company text,val int); insert into com values ('com1',1),('com1',2),('com1',3),('com1',4),('com1',5); insert into com values ('com2',11),('com2',22),('com2',33),('com2',44),('com2',55); insert into com values ('com3',111),('com3',222),('com3',333),('com3',444),('com3',555); I want to get the top 3 value of each company, expected output is : company val --------------- com1 5 com1 4 com1 3 com2 55 com2 44 com2 33 com3 555 com3 444 com3

Best way to use hibernate for complex queries like top N per group

人走茶凉 提交于 2019-12-01 08:16:01
问题 I'm working now for a while on a reporting applications where I use hibernate to define my queries. However, more and more I get the feeling that for reporting use cases this is not the best approach. The queries only result partial columns, and thus not typed objects (unless you cast all fields in java). It is hard to express queries without going straight into sql or hql. My current problem is that I want to get the top N per group, for example the last 5 days per element in a group, where

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

拈花ヽ惹草 提交于 2019-12-01 07:12:46
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 will be hitting the site and the scrape may possibly be in the middle of completing? For example I could

Multiple max values in a query

纵然是瞬间 提交于 2019-12-01 04:36:46
I know the title does not sound very descriptive, but it is the best I could think of: I have this table ID BDATE VALUE 28911 14/4/2009 44820 28911 17/4/2009 32240 28911 20/4/2009 30550 28911 22/4/2009 4422587,5 28911 23/4/2009 4441659 28911 24/4/2009 7749594,67 38537 17/4/2009 58280 38537 20/4/2009 137240 38537 22/4/2009 81098692 38605 14/4/2009 2722368 38605 20/4/2009 5600 38605 22/4/2009 1625400 38605 23/4/2009 6936575 which is in fact a very complicated query encapsulated in a view, but it is not of the matter now. I would like to have for each ID, the row containing the highest BDate. In

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

佐手、 提交于 2019-12-01 04:22:48
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: SELECT id, productId, min(orderIndex) FROM table WHERE rejected = 0 GROUP BY productId This one don't

Select distinct rows whilst grouping by max value

时光毁灭记忆、已成空白 提交于 2019-12-01 04:02:55
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 | 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

MySQL: Limiting number of results received based on a column value | Combining queries

我们两清 提交于 2019-12-01 03:57:27
I've done research on this problem, but am having trouble finding a solution. I have the following query that gives me a list of "some_id"s: SELECT some_id FROM example GROUP BY some_id And I have the following query that will get a list of the 5 most recent entries for a row that has "some_id" equal to a number. SELECT * FROM example WHERE some_id = 1 ORDER BY last_modified DESC LIMIT 5 How can I get the the top 5 most recent entries from the table "example" for each "some_id", using only one query? If there are less than 5 entries for a "some_id", it is okay to include them, if that makes

MYSQL Query - Get latest comment related to the post

爷,独闯天下 提交于 2019-12-01 03:20:12
I am trying to get the latest 1 or 2 comments related to each post I download, a bit like instagram does as they show the latest 3 comments for each post, So far I am getting the posts & the likes counts. Now all I need to do is figure out how to get the latest comments, not too sure how to approach it and that is why I hoping someone with a lot more expertise can help me! This is my current query: (SELECT P.uuid, P.caption, P.imageHeight, P.path, P.date, U.id, U.fullname, U.coverImage, U.bio, U.username, U.profileImage, coalesce(Activity.LikeCNT,0), Activity.CurrentUserLiked FROM USERS AS U

Multiple max values in a query

笑着哭i 提交于 2019-12-01 02:25:18
问题 I know the title does not sound very descriptive, but it is the best I could think of: I have this table ID BDATE VALUE 28911 14/4/2009 44820 28911 17/4/2009 32240 28911 20/4/2009 30550 28911 22/4/2009 4422587,5 28911 23/4/2009 4441659 28911 24/4/2009 7749594,67 38537 17/4/2009 58280 38537 20/4/2009 137240 38537 22/4/2009 81098692 38605 14/4/2009 2722368 38605 20/4/2009 5600 38605 22/4/2009 1625400 38605 23/4/2009 6936575 which is in fact a very complicated query encapsulated in a view, but