greatest-n-per-group

Using Max() function to select group values

谁说胖子不能爱 提交于 2019-11-28 14:44:27
I've got a table like this: SKU ITEM VALUE 1503796 1851920 0,9770637 1503796 1636691 0,9747891 1503796 1503781 0,9741025 1503796 3205763 0,9741025 1503801 1999745 0,9776622 1503801 1999723 0,9718825 1503801 3651241 0,9348839 1503801 1773569 0,9331309 1503811 1439825 0,97053134 1503811 1636684 0,96297866 1503811 1636671 0,96003973 1503811 1600553 0,9535771 1503818 1636708 0,9440251 1503818 1636709 0,9440251 1503818 1779789 0,9423958 1503818 3322310 0,9369579 I need to get output like this (grouped with max value): SKU ITEM VALUE 1503796 1851920 0,9770637 1503801 1999745 0,9776622 1503811

Top N per Group Sql problem in mysql

巧了我就是萌 提交于 2019-11-28 14:37:35
Please I am having a problem querying for the Top N per category from a data set resembling the one shown below. I have see various thread on this but I am having problem adapting their query to my specific problem. +----+---------------------------------+-------+ | ID | Prod |Cat Id | +----+---------------------------------+-------+ | 1 | kntrn | 1 | | 2 | kntrn e | 1 | | 3 | e spl | 1 | | 4 | spl php | 1 | | 5 | php cicarredgtal | 1 | | 6 | cicarredgtal servecounterstrike | 1 | | 7 | servecounterstrike com | 1 | | 8 | zlv | 2 | | 9 | zlv enter | 2 | | 10 | spl php | 2 | +----+---------------

SQLAlchemy: show only latest result if a join returns multiple results

你说的曾经没有我的故事 提交于 2019-11-28 12:38:47
问题 I'm trying to create a Flask app that shows the latest score from individual players. So a player can have multiple scores, but on the leaderboard I only want to show her most recent score. My models.py: class Player(db.Model): __tablename__ = 'player' id = db.Column(db.Integer, primary_key=True) firstname = db.Column(db.String, nullable=False) score = db.relationship('Score', backref='player', lazy='dynamic') def __init__(self, firstname): self.firstname = firstname def __repr__(self):

Db2 - Returning the top 5 of each category

别说谁变了你拦得住时间么 提交于 2019-11-28 12:25:21
问题 I want to be able to return 5 menuitem per menu. Here are the tables menus ------- menuid int() profileName varchar(35) menuitems ----------- itemid int() name varchar(40) menuid int() I do see a solution for MySQL in this thread - mySQL Returning the top 5 of each category, looking for similar solution for DB2. Any suggestion is great appreciated. 回答1: something like: select ... from ( select ..., row_number() over ( partition by m.menuid order by ? ) as rn from menus m join menuitems mi m

Retrieve 2 last posts for each category

耗尽温柔 提交于 2019-11-28 11:51:04
Lets say I have 2 tables: blog_posts and categories. Each blog post belongs to only ONE category, so there is basically a foreign key between the 2 tables here. I would like to retrieve the 2 lasts posts from each category, is it possible to achieve this in a single request? GROUP BY would group everything and leave me with only one row in each category. But I want 2 of them. It would be easy to perform 1 + N query (N = number of category). First retrieve the categories. And then retrieve 2 posts from each category. I believe it would also be quite easy to perform M queries (M = number of

Select all threads and order by the latest one

大城市里の小女人 提交于 2019-11-28 11:12:26
问题 Now that I got the Select all forums and get latest post too.. how? question answered, I am trying to write a query to select all threads in one particular forum and order them by the date of the latest post (column "updated_at"). This is my structure again: forums forum_threads forum_posts ---------- ------------- ----------- id id id parent_forum (NULLABLE) forum_id content name user_id thread_id description title user_id icon views updated_at created_at created_at updated_at last_post_id

MYSQL - Join most recent matching record from one table to another

ぐ巨炮叔叔 提交于 2019-11-28 10:35:54
I have two tables that look like this: Table: cases id name status case_no Table: notes id case_id note_date notes I'd like to be able to create a query that grabs the data from the cases table and only the most recent entry from the notes table for each row in the cases table. So far I'm having no luck at all. Any pointers would be greatly appreciated This will return only the cases with notes attached: SELECT c.*, x.* FROM CASES c JOIN NOTES x ON x.case_id = c.case_id JOIN (SELECT n.case_id, MAX(n.note_date) AS max_note_date FROM NOTES n GROUP BY n.case_id) y ON y.case_id = x.case_id AND y

R: group-wise min or max

依然范特西╮ 提交于 2019-11-28 10:15:02
问题 There are so many posts on how to get the group-wise min or max with SQL. But how do you do it in R? Let's say, you have got the following data frame ID | t | value a | 1 | 3 a | 2 | 5 a | 3 | 2 a | 4 | 1 a | 5 | 5 b | 2 | 2 b | 3 | 1 b | 4 | 5 For every ID, I don't want the min t, but the value at the min t. ID | value a | 3 b| 2 回答1: df is your data.frame - library(data.table) setDT(df) # convert to data.table in place df[, value[which.min(t)], by = ID] Output - > df[, value[which.min(t)],

SQL join to correlated subquery where tables are related by overlapping ranges

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 09:56:59
问题 I have the following table structure: Item ID | Name -------- 1 | Apple 2 | Pear 3 | Banana 4 | Plum 5 | Tomato Event ItemStart | ItemEnd | EventType | EventDate -------------------------------------------- 1 | 2 | Planted | 2014-01-01 1 | 3 | Picked | 2014-01-02 3 | 5 | Eaten | 2014-01-05 The two tables are linked only by the primary key of Item and the range of ItemStart and ItemEnd (inclusive) in Event. Events always refer to contiguous sequences of Items, but not all the Events for a

Get Max value and corresponding column

纵饮孤独 提交于 2019-11-28 09:38:26
问题 How can I get corresponding columns from a max query in mysql? I want find out how many wins a player has. I will find that out by doing a count of the number of games that player has won. The will be done by selecting the max value per game and resulting player_id. However I am not sure how to get the corresponding player_id. I have id | game_id | player_id | score 1 | 1 | 1 | 254 2 | 1 | 2 | 194 3 | 2 | 1 | 432 4 | 2 | 2 | 298 回答1: This query should get what you need: SELECT player_id, game